0

我想使用 extjs6 现代工具包上传文件。因此我MessageBox用文件选择器显示一个。单击“确定”按钮上传(例如通过 HTTP POST)后,如何将所选文件检索到 javascript 对象中?

this.createUploadMsgBox("File Upload", function (clickedButton) {
    if (clickedButton == 'ok') {
        console.log("file: " + file);
    }

createUploadMsgBox: function (title, callback) {
        Ext.Msg.show({
            title: title,
            width: 300,
            buttons: Ext.MessageBox.OKCANCEL,
            fn: callback,
            items: [
                {
                    xtype: 'filefield',
                    label: "File:",
                    name: 'file'
                }
            ]
        });
    }

你可以在这里朗姆我的例子:

https://fiddle.sencha.com/#view/editor&fiddle/1kro

4

1 回答 1

1

你有两个可能的解决方案。

一种是使用form, 并通过form.submit()form.isValid()在提交前使用)发送文件。您可以使用 MultipartFile 检索服务器中的文件。

另一种方法是使用JS File API。在您createUploadMsgBox的功能中:

this.createUploadMsgBox("File Upload", function (clickedButton) {
   if (clickedButton == 'ok') {
      //console.log("file: " + file);
      var filefield = Ext.ComponentQuery.query('filefield')[0];
      var file = filefield.el.down('input[type=file]').dom.files[0];
      var reader = new FileReader();
      reader.onload = (function(theFile) {
          return function(e) {
              console.log(e.target.result);
          };
      })(file);
      reader.readAsBinaryString(file);
   }
});

file对象中你有文件的基本信息,然后你会在控制台中看到文件的内容。

希望这可以帮助!

于 2016-11-22T08:52:02.333 回答