【问题标题】:ExtJS file upload to ASP.net MVC status OK but no content being sentExtJS 文件上传到 ASP.net MVC 状态正常但没有发送内容
【发布时间】:2014-03-18 13:57:35
【问题描述】:

我正在尝试将文件从 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 警报,标题为“失败”,消息为“postedFile == null”(请参阅​​case Ext.form.action.Action.SERVER_INVALID)。我可以做些什么来确保文件内容实际被发送并且服务器端的Request.Files["file"]not 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\"}");
        }
    }

【问题讨论】:

    标签: c# javascript asp.net-mvc file-upload extjs4


    【解决方案1】:

    看看这个answer

    您需要在控制器操作参数中使用 HttpPostedFileBase 才能获取您已发布的文件数据。

    【讨论】:

      猜你喜欢
      • 2012-04-27
      • 1970-01-01
      • 1970-01-01
      • 2019-09-29
      • 2019-03-16
      • 2021-07-02
      • 2010-10-16
      • 1970-01-01
      • 2017-05-29
      相关资源
      最近更新 更多