2

使用以下代码,我不断收到错误消息:无法将类型“System.Net.Http.MultipartFormDataStreamProvider”隐式转换为“System.Threading.Tasks.Task>”

MultipartFormDataStreamProvider streamProvider = new MultipartFormDataStreamProvider("c:\\tmp\\uploads");

//Error line
Task<IEnumerable<HttpContent>> bodyparts =  await Request.Content.ReadAsMultipartAsync(streamProvider);

我认为这是一个容易的,但我错过了。

4

1 回答 1

7

我最终使用了它,效果很好。我在这里找到了

http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2

using System.Diagnostics;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

public class UploadController : ApiController
{
public async Task<HttpResponseMessage> PostFormData()
{
    // Check if the request contains multipart/form-data.
    if (!Request.Content.IsMimeMultipartContent())
    {
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

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

    try
    {
        // Read the form data.
        await Request.Content.ReadAsMultipartAsync(provider);

        // 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);
    }
    catch (System.Exception e)
    {
        return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
    }
}

}

于 2012-09-25T17:26:37.137 回答