C# ExtJS文件上载到ASP.net MVC状态正常,但未发送任何内容
我正在尝试将一个文件从ExtJS 4.2前端上载到C#/ASP.net MVC 2后端。我的前端代码很大程度上是基于以下ExtJS文档中的代码建模的:。选择文件并单击上载按钮后,请求状态为200,服务器端往返成功,但服务器端代码表明request.Files[“file”]为空 使用Chrome dev工具,显然正在尝试上载文件,但边界之间没有内容:C# ExtJS文件上载到ASP.net MVC状态正常,但未发送任何内容,c#,javascript,asp.net-mvc,file-upload,extjs4,C#,Javascript,Asp.net Mvc,File Upload,Extjs4,我正在尝试将一个文件从ExtJS 4.2前端上载到C#/ASP.net MVC 2后端。我的前端代码很大程度上是基于以下ExtJS文档中的代码建模的:。选择文件并单击上载按钮后,请求状态为200,服务器端往返成功,但服务器端代码表明request.Files[“file”]为空 使用Chrome dev工具,显然正在尝试上载文件,但边界之间没有内容: ------WebKitFormBoundaryh2Q8Ya8kWQkiSLOb Content-Disposition: form-data;
------WebKitFormBoundaryh2Q8Ya8kWQkiSLOb
Content-Disposition: form-data; name="users"; filename="0_USER_INFO.xlsx"
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
------WebKitFormBoundaryh2Q8Ya8kWQkiSLOb--
下面是我的客户端和服务器端代码的相关部分。当前,当我单击upload时,我收到一个ExtJS警报,标题为“Failure”,消息为“postedFile==null”(请参见case Ext.form.action.action.SERVER\u INVALID
)。如何确保文件内容实际被发送,并且服务器端的Request.Files[“file”]
不为空
客户端代码:
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\"}");
}
}
看看这个
您需要在控制器操作参数中使用HttpPostedFileBase,以获取已发布的文件数据