将多个文件作为流处理而不保存在内存中

时间:2018-09-03 04:15:56

标签: asp.net-web-api

是否可以以及如何处理多个文件上传?标头Content-Type: form-data/multipart。我在官方站点的示例中看到了下一个代码:

public Task<HttpResponseMessage> PostFormData()
{

if (!Request.Content.IsMimeMultipartContent())
{
    throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}

string root = HttpContext.Current.Server.MapPath("~/App_Data");
var provider = new MultipartFormDataStreamProvider(root);

// Read the form data and return an async task.
var task = Request.Content.ReadAsMultipartAsync(provider).
    ContinueWith<HttpResponseMessage>(t =>
    {
        if (t.IsFaulted || t.IsCanceled)
        {
            Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
        }

        // This illustrates how to get the file names.
        foreach (MultipartFileData file in provider.FileData)
        {
            Trace.WriteLine(file.Headers.ContentDisposition.FileName);
            Trace.WriteLine("Server file path: " + file.LocalFileName);
        }
        return Request.CreateResponse(HttpStatusCode.OK);
    });

return task;
}

如果我理解正确,它将文件保存在本地计算机内存中。是否可以将文件作为流处理而不保存在内存中?

0 个答案:

没有答案