问题描述
我将如何去在使用 MultipartFormDataStreamProvider
和 Request.Content.ReadAsMultipartAsync
ApiController
?
How would i go about using MultipartFormDataStreamProvider
and Request.Content.ReadAsMultipartAsync
in a ApiController
?
我用Google搜索了一些教程,但我不能得到其中的任何工作,使用.NET 4.5 IM。
I have googled a few tutorial but i can't get any of them to work, im using .net 4.5.
这是我目前得到了:
public class TestController : ApiController
{
const string StoragePath = @"T:\WebApiTest";
public async void Post()
{
if (Request.Content.IsMimeMultipartContent())
{
var streamProvider = new MultipartFormDataStreamProvider(Path.Combine(StoragePath, "Upload"));
await Request.Content.ReadAsMultipartAsync(streamProvider);
foreach (MultipartFileData fileData in streamProvider.FileData)
{
if (string.IsNullOrEmpty(fileData.Headers.ContentDisposition.FileName))
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
}
string fileName = fileData.Headers.ContentDisposition.FileName;
if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
{
fileName = fileName.Trim('"');
}
if (fileName.Contains(@"/") || fileName.Contains(@"\"))
{
fileName = Path.GetFileName(fileName);
}
File.Copy(fileData.LocalFileName, Path.Combine(StoragePath, fileName));
}
}
else
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));
}
}
}
我得到的异常
意外结束。 MIME多消息不是
完整的。
在等待任务;
运行。
没有任何一个有任何想法我做错了,或者使用网络API有一个正常的asp.net项目工作的例子。
when the await task;
runs.Does any one have any idea what i am doing wrong or have a working example in a normal asp.net project using web api.
推荐答案
我解决了这个错误,我不明白这与多部分流的末尾做的,但这里的工作code:
I resolved the error, i don't understand what this has to do with end of multipart stream but here is the working code:
public class TestController : ApiController
{
const string StoragePath = @"T:\WebApiTest";
public async Task<HttpResponseMessage> Post()
{
if (Request.Content.IsMimeMultipartContent())
{
var streamProvider = new MultipartFormDataStreamProvider(Path.Combine(StoragePath, "Upload"));
await Request.Content.ReadAsMultipartAsync(streamProvider);
foreach (MultipartFileData fileData in streamProvider.FileData)
{
if (string.IsNullOrEmpty(fileData.Headers.ContentDisposition.FileName))
{
return Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted");
}
string fileName = fileData.Headers.ContentDisposition.FileName;
if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
{
fileName = fileName.Trim('"');
}
if (fileName.Contains(@"/") || fileName.Contains(@"\"))
{
fileName = Path.GetFileName(fileName);
}
File.Move(fileData.LocalFileName, Path.Combine(StoragePath, fileName));
}
return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted");
}
}
}
这篇关于使用MultipartFormDataStreamProvider和ReadAsMultipartAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!