Tôi đang cố gắng lưu (một) tệp đã tải lên vào cơ sở dữ liệu/bộ nhớ, nhưng tôi không thể tìm ra.ASP .Net Web API RC: Multipart tải tập tin lên Memorystream
Tất cả tôi có ngay bây giờ là thế này:
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;
}
Nhưng tất nhiên điều này chỉ lưu các tập tin vào một vị trí cụ thể. Tôi nghĩ rằng tôi phải làm việc với một lớp tùy chỉnh có nguồn gốc từ MediaTypeFormatter để lưu nó vào một MemoryStream, nhưng tôi không thấy làm thế nào để làm điều đó.
Vui lòng trợ giúp. Cảm ơn trước!
Liệu nó có nghĩa là nội dung của từng phần sẽ được nạp vào bộ nhớ? Nếu bạn có các tệp lớn đang được tải lên thì điều đó có nghĩa là bạn phải viết thực thi 'MultipartStreamProvider' của riêng mình? –
Bit '// do something' trông như thế nào ...? – simonlchilds