我正在尝试从多部分表单中读取上传的文件:
<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