我正在尝试将(一个)上传的文件保存到数据库/内存流中,但我无法弄清楚。
我现在只有这个:
public Task<HttpResponseMessage> AnswerQuestion()
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
var root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);
var task = Request.Content.ReadAsMultipartAsync(provider).
ContinueWith<HttpResponseMessage>(t =>
{
if (t.IsFaulted || t.IsCanceled)
{
Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
}
foreach (var file in provider.FileData)
{
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine("Server file path: " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
});
return task;
}
但当然这只会将文件保存到特定位置。我想我必须使用从 MediaTypeFormatter 派生的自定义类才能将其保存到 MemoryStream,但我不知道该怎么做。
请帮忙。提前致谢!