0

我正在尝试使用以下代码上传文件:

window = Ext.create('Ext.window.Window', {
        renderTo: Ext.getBody(),
        bodyPadding: '20 10',
        title: 'Upload file',
        autoDestroy: true,
        hidden: true,
        modal: true,
        layout: {
            type: 'hbox',
            align: 'middle',
            pack: 'center'
        },
        items: [
            uploadForm = new Ext.form.Panel({
                items: [
                    file = new Ext.form.field.File({
                        xtype: 'filefield',
                        name: 'fileName',
                        fieldLabel: 'File',
                        allowBlank: false,
                        buttonText: 'Select file...',
                    })
                ]
            })
        ],
        buttons: [{
                text: 'Cancel',
                handler: function () {
                    upload.hide();
                }
            },
            {
                text: 'Upload',
                handler: function () {
                    var form = uploadForm.getForm();
                    if (form.isValid()) {
                        form.submit({
                            url: 'upload',
                            waitMsg: 'Uploading your file...',
                            scope: this,
                            success: function (form, action) {
                                upload.close();
                                var json = JSON.parse(action.response.responseText);
                                if (json.success) {
                                     Ext.Msg.alert('Success', json.message);
                                } else {
                                    Ext.Msg.alert('Error', json.message);
                                }
                            },
                            failure: function (form, action) {
                                upload.close();
                                try {
                                    var json = JSON.parse(action.response.responseText);
                                    Ext.create('Ext.window.MessageBox').show({
                                        title: 'Failure',
                                        msg: json.message
                                    });
                                } catch (err) {
                                    Ext.create('Ext.window.MessageBox')
                                                    .alert('Failure', 'Failed to parse response');
                                }
                            }
                        });
                    }
                }
            }]
    });

该代码在 firefox 和 opera 中运行,我成功获得了响应,但在 chrome 中检查网络活动时,状态被取消,并且在控制台中我收到警告:资源解释为文档,但使用 MIME 类型应用程序/json 传输。因此,提交总是返回失败,即使文件已上传。谁能建议如何解决这个问题?

4

0 回答 0