【问题标题】:How to access additional form data sent from jquery file upload in asp.net core?如何访问从asp.net核心中的jquery文件上传发送的附加表单数据?
【发布时间】:2019-05-01 18:09:20
【问题描述】:

我正在使用 jquery 文件上传将大文件分块上传到服务器。我需要将附加数据连同文件一起发送到服务器。

我发现this 文章建议如下添加formData。

$form = $('#fileupload').fileupload({
            maxChunkSize: 3000000,
            url: '/FileAPI/Upload',
            formData: { example: 'test' }
         });

如何在 asp.net core 中从 HttpContext.Request 访问 formData?它在哪里可用? 感谢您的任何建议。

[HttpPost]
[Route("Upload")]

public ActionResult Upload()
{
    var CurrentContext = HttpContext.Request;
}

【问题讨论】:

  • 表单数据是模型(在您的模型文件夹中)吗?我可以建议在您的ActionResult 中添加[FromBody] myclass formdata 作为输入吗?

标签: c# asp.net-core jquery-file-upload


【解决方案1】:

只需将其添加到函数签名中

这应该可以工作

[HttpPost]
[Route("Upload")]
public ActionResult Upload(IEnumerable<IFormFile> files, string example)
{   
   //do something with data
}

【讨论】:

    【解决方案2】:

    如何在 asp.net core 中从 HttpContext.Request 访问 formData?

    根据您提供的文章,“formData 对象,formData: {example: 'test'} 将作为 POST 参数到达服务器端”

    您也可以尝试以下方法获取:

     [HttpPost]
     [Route("Upload")]
     public void Upload()
     {
            var dict = Request.Form.ToDictionary(x => x.Key, x => x.Value.ToString());
           //In that case, you could iterate over your dictionary or you can access values directly:
            var CurrentContext = dict["example"];
      }
    

    【讨论】:

      猜你喜欢
      • 2017-04-20
      • 2015-09-15
      • 2017-09-26
      • 2017-10-31
      • 2021-08-30
      • 1970-01-01
      • 2011-11-03
      • 2016-12-06
      相关资源
      最近更新 更多