在Http标头中找不到FileName

时间:2016-05-18 14:59:58

标签: c# asp.net asp.net-web-api

我正在尝试从多部分表单中读取上传的文件:

<form action="http://localhost:17034/api/v1/reports/create" method="post" enctype="multipart/form-data">
    <input type="text" name="myFile" id="myFile" /><br />
    <input type="text" name="Description" id="Description" /><br />
    <input type="file" name="Content" id="content" />
    <input type="submit" value="Submit" />
</form>

我的控制器操作如下:

[HttpPost]
[Route("reports/create")]
public async Task<HttpResponseMessage> Create()
{
   var provider = new MultipartMemoryStreamProvider();
   await Request.Content.ReadAsMultipartAsync(provider);

   foreach (HttpContent ctnt in provider.Contents)
   {
      //I want to ensure I read text files as a stream so want
      //to check the file extension
      var fileName = ctnt.Headers.ContentDisposition.FileName //NULL FOR FILE INPUT
   }

如果我在提交表单时检查文件输入的内容处理方式,我会得到:

{form-data; name="myFile"}
CreationDate: null
DispositionType: "form-data"
FileName: null
FileNameStar: null
ModificationDate: null
Name: "\"myFile\""
Parameters: Count = 1
ReadDate: null
Size: null
dispositionType: "form-data"
parameters: Count = 1

1 个答案:

答案 0 :(得分:0)

  

{形式数据; NAME = “MYFILE”}
  姓名:“\”myFile \“”

这是因为您正在检查名称为myFile的表单数据。您应该检查名为Content

的那个

enter image description here