当文件作为数组通过时,Request.Files为空 [英] Request.Files is empty when files come through as an array

查看:63
本文介绍了当文件作为数组通过时,Request.Files为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与第三方表单生成器软件进行一些集成,该软件可将表单发布到我们自己的服务器上.然后将表单数据和文件保存到DB.问题是当表单包含多个文件上载字段时, Request.Files 始终为空.

I'm doing some integration with a third party form builder software that allows the form to be posted to our own server. The form data and files are then saved to a DB. The issue is when the form contains multiple file upload fields, Request.Files is always empty.

使用Fiddler,我可以看到二进制文件通过.我唯一想到的是字段名称中包含方括号(因为它是作为数组发送的),因此模型绑定程序无法正确绑定它?通过的字段名称是 tfa_20 [0] tfa_20 [1] .

Using Fiddler, I can see the binary files coming through. The only thing that I can think of is that the field name contains brackets in them (because it's being sent as an array) and so the model binder can't bind it properly? The field names that are coming through are tfa_20[0] and tfa_20[1].

就代码而言,这是非常标准的东西:

Code-wise, it's pretty standard stuff:

var data = new Submission()
{
    ConfigurationDetailId = configDetail.Id,
    SubmitterEmail = submitterEmail,
    SubmissionData = Request.Form.AllKeys.Select(k => new SubmissionData()
    {
        FieldName = k,
        FieldValue = Request.Form[k]
    }).ToList(),
    SubmissionFiles = new List<SubmissionFile>()
};

// process any files uploaded
if (Request.Files.Count > 0)
{
    foreach (string field in Request.Files)
    {
        var uploadedFile = Request.Files[field];

        if (!string.IsNullOrEmpty(fileName))
        {
                data.SubmissionFiles.Add(GetSubmissionFile(uploadedFile, fileName));
        }
    }
}

Repository.SaveForm(data);

任何帮助将不胜感激.

推荐答案

使用HttpPostedFileBase可以将文件发布到您的操作中.如果有多个文件,则应使用HttpPostedFileBase []数组.

Use HttpPostedFileBase in order to get posted file to your action. in case of multiple files,should be used an HttpPostedFileBase[] array.

要启用表单上载功能,必须将enctype ="multipart/form-data"添加到表单标签中.或者,如果您使用剃刀语法,请将beginForm标记更改为此.

To Enable uploading in forms, is necessary to add enctype="multipart/form-data" to your form tag. or if you use razor syntax change your beginForm tag to this.

View.cshtml

View.cshtml

   @using (Html.BeginForm("action","controller", FormMethod.Post, new { @enctype = 
  "multipart/form-data" }))
    {
    }


 public ActionResult YourAction(HttpPostedFileBase[] files)
    {
        var data = new Submission()
        {
            ConfigurationDetailId = configDetail.Id,
            SubmitterEmail = submitterEmail,
            SubmissionData = Request.Form.AllKeys.Select(k => new SubmissionData()
            {
                FieldName = k,
                FieldValue = Request.Form[k]
            }).ToList(),
            SubmissionFiles = new List<SubmissionFile>()
        };

        if (files.Length > 0)
        {
            foreach (HttpPostedFileBase file in files)
            {
                var uploadedFile = file;

                if (!string.IsNullOrEmpty(file.FileName))
                {
                    data.SubmissionFiles.Add(GetSubmissionFile(uploadedFile, file.fileName));
                }
            }
        }

        return View();
    }

这篇关于当文件作为数组通过时,Request.Files为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆