web api 上传

using System.Net ;
using System.Net.Http ;
using System.Web ;
using System.Web.Http ;
 
namespace FileUploadTest.Controllers
{
public class FileUploadController : ApiController

{

    public async Task<HttpResponseMessage> Post()

    {

        if (Request.Content.IsMimeMultipartContent())

        {

            var path = HttpContext.Current.Server.MapPath("~/App_Data");

            var provider = new MultipartFormDataStreamProvider(path);

            await Request.Content.ReadAsMultipartAsync(provider).ContinueWith(t =>

            {

                if (t.IsFaulted || t.IsCanceled)

                    throw new HttpResponseException(HttpStatusCode.InternalServerError);

            });

            //Here you should return a meaningful response

            return Request.CreateResponse(HttpStatusCode.OK); 

        }

        else

        {

            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "This request is not properly formatted"));

        }

    }

}
}

你可能感兴趣的:(Web)