This article was reproduced from: http://www.cnblogs.com/zj1111184556/p/3494502.html
Public classFileuploadcontroller:apicontroller { Public AsyncTaskPost () {//Check if it is Multipart/form-data if(! Request.Content.IsMimeMultipartContent ("Form-data")) Throw Newhttpresponseexception (Httpstatuscode.unsupportedmediatype); Httpresponsemessage Response=NULL; Try { //Set upload directory varProvider =NewMultipartformdatastreamprovider (@"F:\\studyproject\\webapi2demo\\csdemo\\upload"); //receive data and save the file varBodyparts =awaitRequest.Content.ReadAsMultipartAsync (provider); Response=Request.createresponse (httpstatuscode.accepted); } Catch { Throw Newhttpresponseexception (httpstatuscode.badrequest); } returnresponse; } }
Public classFileuploadcontroller:apicontroller { Public stringPost () {Httppostedfile file= httpcontext.current.request.files[0]; stringstrpath ="D:\\myprojects\\studysolution\\restdemo\\upload\\test2.rar" ; File. SaveAs (strpath); stringresult ="0"; returnresult; } }
Go WebApi Uploading Files