2

我有一个简单的 ExtJs 表单面板,其中包含一个文件上传字段。当我将表单提交到服务器时,所有键值表单值都会发送到服务器,但不会发送文件上传字段的键值对。有人知道为什么吗?(我在下面附上了一些代码片段)

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

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

2 回答 2

4

您无法获取文件方法参数。因此,您的 photopath 变量通常为空。要访问上传的文件,您可以执行以下操作:

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

在 javascript 中,您需要添加fileUpload: true,到表单配置中,以便 ExtJS 知道您正在上传表单中的文件。

于 2011-03-29T05:21:28.440 回答
1

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

Response.ContentType = "text/html";

如果您阅读 Ext.data.Connection 的文档,您会看到

浏览器解析服务器响应以创建 IFRAME 的文档。如果服务器使用 JSON 发送返回对象,则 Content-Type 标头必须设置为“text/html”,以便告诉浏览器将未更改的文本插入到文档正文中。

我花了一段时间才找到这个,但是当我遇到你的问题时,其他人可能会遇到类似的问题!

希望这会帮助他们!

于 2011-06-03T13:14:35.197 回答