如何接受文件发布我正在使用ASP.NETMVC4webapibeta来构建REST服务。我需要能够接受张贴的图像/文件从客户端应用程序。这有可能使用webapi吗?下面是我目前正在使用的操作。有谁知道这个应该如何运作的例子吗?[HttpPost]public string ProfileImagePost(HttpPostedFile profileImage){
string[] extensions = { ".jpg", ".jpeg", ".gif", ".bmp", ".png" };
if (!extensions.Any(x => x.Equals(Path.GetExtension(profileImage.FileName.ToLower()), StringComparison.OrdinalIgnoreCase)))
{
throw new HttpResponseException("Invalid file type.", HttpStatusCode.BadRequest);
}
// Other code goes here
return "/path/to/image.png";}
3 回答
慕丝7291255
TA贡献1859条经验 获得超6个赞
[HttpPost, Route("api/upload")]public async Task<IHttpActionResult> Upload(){ if (!Request.Content.IsMimeMultipartContent()) throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); var provider = new MultipartMemoryStreamProvider(); await Request.Content.ReadAsMultipartAsync(provider); foreach (var file in provider.Contents) { var filename = file.Headers.ContentDisposition.FileName.Trim('\"'); var buffer = await file.ReadAsByteArrayAsync(); //Do whatever you want with filename and its binary data. } return Ok();}
- 3 回答
- 0 关注
- 394 浏览
添加回答
举报
0/150
提交
取消