File upload in extjs 4.2 without form.submit()

后端 未结 4 2187
闹比i
闹比i 2021-01-05 16:17

I\'m trying to upload a file (as of now of any extension) in extjs. I have a model and store. the file upload happens from a window and I dont have a form in the window. All

4条回答
  •  误落风尘
    2021-01-05 16:30

    If you want to still use ExtJS's fileuploadfield and upload through an AJAX call using HTML5 FileReader, you can do it like such:

    launchUpload: function () {
        //get a handle of the "file" input in the widget itself...
        var fileInput = document.getElementById(yourUploadField.button.fileInputEl.id);
        var fileReader = New FileReader();
        var fileToUpload = fileInput.files[0]; //assuming your only uploading one file...
        var me = this
    
        fileReader.onload = function (e) {
             me.onLoadFile(e, me, fileToUpload.name);
        }
    
        fileReader.readAsDataURL(fileToUpload);
    
    }, 
    onLoadFile: function (e, scope, filename) {
    
         //I carry the scope around for functionality...
    
         Ext.Ajax.request({
            method: 'POST',
            url: 'url',
            scope: scope,
            jsonData: { fileNameParameter: filename, fileDatainBase64: e.target.result},
            success: function (response, operation) {
                //success..
            },
            failure: function (response, operation) {
                //failure...
            }
        });       
    
    }
    

提交回复
热议问题