ExtJS文件上载到ASP.net MVC状态OK,但没有内容正在发送

ExtJS file upload to ASP.net MVC status OK but no content being sent

本文关键字:上载 文件 ASP net OK 状态 MVC ExtJS      更新时间:2023-09-26

我试图从ExtJS 4.2前端上传到c#/ASP.net MVC 2后端。我的前端代码很大程度上是基于ExtJS文档的:http://docs.sencha.com/extjs/4.2.1/#!/api/Ext.form.field.File。选择文件并单击上传按钮后,请求状态为200,服务器端往返成功完成,但服务器端代码指示为request。Files["file"]为空。

使用Chrome开发工具,很明显正在尝试上传文件,但边界之间没有内容:

------WebKitFormBoundaryh2Q8Ya8kWQkiSLOb
Content-Disposition: form-data; name="users"; filename="0_USER_INFO.xlsx"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

------WebKitFormBoundaryh2Q8Ya8kWQkiSLOb--
下面是我的客户端和服务器端代码的相关部分。目前,当我点击上传时,我得到一个ExtJS警报,标题为"失败",msg为"postedFile == null"(见case Ext.form.action.Action.SERVER_INVALID)。我能做些什么来确保文件内容实际上被发送,并且服务器端的Request.Files["file"]而不是 null?

客户端代码:

Ext.define('Tools.view.users.Manager', {
    extend: 'Ext.tab.Panel',
    alias: 'widget.usermanager',
    title: 'User Manager',
    items: [{
        title: 'Import',
        items: [{
            xtype: 'form',
            width: 400,
            bodyPadding: 10,
            //frame: true,
            renderTo: Ext.getBody(),
            items: [{
                xtype: 'filefield',
                name: 'users',
                fieldLabel: 'New Users',
                labelWidth: 75,
                msgTarget: 'side',
                allowBlank: false,
                anchor: '100%',
                buttonText: 'From Excel'
            }],
            buttons: [{
                text: 'Upload',
                handler: function () {
                    var form = this.up('form').getForm();
                    if (form.isValid()) {
                        form.submit({
                            url: '/User/Upload',
                            waitMsg: 'Uploading user data...',
                            success: function (form, action) {
                                Ext.Msg.alert('Success', action.result.msg);
                            },
                            failure: function (form, action) {
                                switch (action.failureType) {
                                    case Ext.form.action.Action.CLIENT_INVALID:
                                        Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                                        break;
                                    case Ext.form.action.Action.CONNECT_FAILURE:
                                        Ext.Msg.alert('Failure', 'Ajax communication failed');
                                        break;
                                    case Ext.form.action.Action.SERVER_INVALID:
                                        Ext.Msg.alert('Failure', action.result.msg);
                                }
                            }
                        });
                    }
                }
            }]
        }]
    }]
});
服务器端:

public class UserController : Controller
{
    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Upload()
    {
        HttpPostedFileBase postedFile = Request.Files["file"];
        if (postedFile == null)
        {
            return Content("{'"success'": false, '"msg'": '"postedFile == null'"}");
        }
        else
        {
            return Content("{'"success'": true, '"msg'": '"postedFile != null'"}");
        }
    }

看看这个答案。

你需要在控制器动作参数中使用httppostdfilebase来获取你已经发布的文件数据。