HTML表单:
<form id="form1" method="post" enctype="multipart/form-data" action="http://localhost:2317/api/uploadFile"> <div> <label for="Name">Name</label> </div> <div> <input name="Name" type="text" /> </div> <div> <label for="Price">Price</label> </div> <div> <input name="Price" type="text" /> </div> <div> <input name="ImageFile" type="file" id="me"/> </div> <div> <input type="submit" name="name" value="submit" /> </div> </form>
API Controller:
[Route("api/uploadFile")] public HttpResponseMessage PostFile() { HttpResponseMessage result = null; var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count > 0) { var docfiles = new List<string>(); foreach (string file in httpRequest.Files) { var postedFile = httpRequest.Files[file]; var filePath = HttpContext.Current.Server.MapPath("~/" + postedFile.FileName); postedFile.SaveAs(filePath); docfiles.Add(filePath); } result = Request.CreateResponse(HttpStatusCode.Created, docfiles); } else { result = Request.CreateResponse(HttpStatusCode.BadRequest); } return result; }