とをどのように使用しMultipartFormDataStreamProvider
ますか?Request.Content.ReadAsMultipartAsync
ApiController
私はいくつかのチュートリアルをグーグルで検索しましたが、.net4.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マルチパートストリームの予期しない終了。MIMEマルチパートメッセージが完了していません。
実行するときawait task;
。誰かが私が間違っていることを知っているか、WebAPIを使用する通常のasp.netプロジェクトで実際の例を持っていますか?