2011-03-28 48 views
2

我有一个简单的ExtJs表单面板,它包含一个fileuploadfield。当我将表单提交给服务器时,所有的键值表单值都会发送到服务器,而不是文件上传字段的键值对。 有谁知道这是为什么? (我在下面附加了一些代码片段)使用ASP.NET MVC处理服务器上的ExtJs'fileuploadfield'?

另外我该如何处理服务器上的上传。即我想将图像上传到服务器,并将其保存在服务器的某个位置?

public JsonResult SetEmployeeDetails(string firstname, string photopath) 
    { 
     GetData data = delegate 
     { 
      return Repo.SetEmployeeDetails(firstname, photopath); 
     }; 
     JsonResultBase jsonResult = GetJsonResult(data); 
     JsonResult json = PortalJsonResult(jsonResult, JsonRequestBehavior.AllowGet); 
     return json; 
    } 



xtype: 'form', 
title: 'Employee Setup', 
items: [{ 
      fieldLabel: 'Firstname', 
      xtype: 'textfield', 
      name: 'firstname', 
      maxLength: 10, 
      allowBlank:false 
     }, 
     { 
      xtype: 'fileuploadfield', 
      id: 'form-file', 
      emptyText: 'Select an image', 
      fieldLabel: 'Photo', 
      name: 'photopath', 
      buttonText: '', 
      buttonCfg: { 
       iconCls: 'upload-icon' 
      } 
     }], 
buttons: [{ 
    text: 'Save', 
    scope: this, 
    handler: function(){ 
     var form = this.items.items[0].getForm(); 
     if(form.isValid()){ 
      form.submit({ 
       url: 'EmployeeDetails/SetEmployeeDetails', 
       waitMsg: 'Saving your details...', 
       success: function(fp, o){ 
        msg('Success', 'Processed file on the server'); 
       } 
      }); 
     } 
    } 
}] 

回答

4

您无法获取文件方法参数。所以,你的photopath变量通常是空的。 要访问上传的文件,你可以做到以下几点:

HttpPostedFileBase postedFile = Request.Files["fileinput"]; 
if (postedFile != null) 
{ 
    //process the file content using postedFile.InputStream... 
} 

而在JavaScript中,你需要添加fileUpload: true,到您的形式配置,使ExtJS的知道你上传的形式的文件。

1

有类似的问题。发现您需要在处理文件上传的页面上将内容类型设置为“text/html”。 :-(

Response.ContentType = "text/html"; 

如果read the documentation for Ext.data.Connection,你会看到

服务器响应是由浏览器解析以创建IFRAME的文档。如果服务器使用JSON发送返回的对象,那么Content-Type头部必须设置为“text/html”,以便告诉浏览器将文本原样插入文档主体。

花了我一会儿才找到这个,但是当我来在你的问题上,别人可能会做一个类似的问题!

希望这会帮助他们!