zl程序教程

您现在的位置是:首页 >  后端

当前栏目

web api 上传

2023-09-11 14:20:18 时间
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"));
        }
    }
}
}