WEB API 使用MultipartFormDataStreamProvider上传文件
来源:互联网 发布:java运行class文件命令 编辑:程序博客网 时间:2024/05/16 09:35
在这简短的教程中,我们打算去看如何上传多个文件在 ASP.Net WebAPI 中使用 MultipartFormDataStreamProvider。这一概念基于多部分/格式数据我们可以在这里发布多个文件的内容不仅将 NameValueCollection 作为服务器端提供的常规表单字段。在本教程中我们还看到了如何重写默认行为的 MultipartFormDataStreamProvider,将名称存储在一个独特的 BodyPart_ {GUID} 格式中。
using System;using System.Collections.Generic;using System.Diagnostics;using System.IO;using System.Linq;using System.Net;using System.Net.Http;using System.Net.Http.Headers;using System.Threading.Tasks;using System.Web;using System.Web.Http;using System.Web.Mvc;namespace UploadApplication.Controllers{ public class UploadController : ApiController { public async Task<HttpResponseMessage> Post() { // Check whether the POST operation is MultiPart? if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } // Prepare CustomMultipartFormDataStreamProvider in which our multipart form // data will be loaded. string fileSaveLocation = HttpContext.Current.Server.MapPath("~/App_Data"); CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation); List<string> files = new List<string>(); try { // Read all contents of multipart message into CustomMultipartFormDataStreamProvider. await Request.Content.ReadAsMultipartAsync(provider); foreach (MultipartFileData file in provider.FileData) { files.Add(Path.GetFileName(file.LocalFileName)); } // Send OK Response along with saved file names to the client. return Request.CreateResponse(HttpStatusCode.OK, files); } catch (System.Exception e) { return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e); } } } // We implement MultipartFormDataStreamProvider to override the filename of File which // will be stored on server, or else the default name will be of the format like Body- // Part_{GUID}. In the following implementation we simply get the FileName from // ContentDisposition Header of the Request Body. public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider { public CustomMultipartFormDataStreamProvider(string path) : base(path) { } public override string GetLocalFileName(HttpContentHeaders headers) { return headers.ContentDisposition.FileName.Replace("\"", string.Empty); } }}
测试程序
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net.Http;using System.Net.Http.Headers;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { using (var client = new HttpClient()) using (var content = new MultipartFormDataContent()) { // Make sure to change API address client.BaseAddress = new Uri("http://localhost:53798/"); // Add first file content var fileContent1 = new ByteArrayContent(File.ReadAllBytes(@"c:\Users\aisadmin\Desktop\Me\NF2202533167366.pdf")); fileContent1.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Sample.pdf" }; // Add Second file content var fileContent2 = new ByteArrayContent(File.ReadAllBytes(@"c:\Users\aisadmin\Desktop\Sample.txt")); fileContent2.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "Sample.txt" }; content.Add(fileContent1); content.Add(fileContent2); // Make a call to Web API var result = client.PostAsync("/api/upload", content).Result; Console.WriteLine(result.StatusCode); Console.ReadLine(); } } }}
3 0
- WEB API 使用MultipartFormDataStreamProvider上传文件
- WEB API 使用MultipartFormDataStreamProvider上传文件
- 使用Wininet API上传文件到Web Server
- webapi不使用MultipartFormDataStreamProvider上传multipart form-data数据的方法
- jQuery上传文件和Core Web API接收保存文件
- 使用 JavaScript File API 实现文件上传
- 使用 JavaScript File API 实现文件上传
- 使用 JavaScript File API 实现文件上传
- 使用 JavaScript File API 实现文件上传
- 使用 JavaScript File API 实现文件上传
- 使用 JavaScript File API 实现文件上传
- 使用 JavaScript File API 实现文件上传
- 使用 JavaScript File API 实现文件上传
- 使用 JavaScript File API 实现文件上传
- 使用 JavaScript File API 实现文件上传
- Web Api 图片上传
- .net mvc web api上传图片/文件并重命名
- Web Api Self Host大文件上传功能
- UVA 12260 (思路dp)
- 安装好scala后出现“找不到或无法加载主类”的问题
- HUD3038How Many Answers Are Wrong(并查集)经典
- 使用 udev 高效、动态地管理 Linux 设备文件
- 数据挖掘参考资料
- WEB API 使用MultipartFormDataStreamProvider上传文件
- <<搭讪魔法师>> 看 罗辑思维谈
- swift键盘点击屏幕后返回
- Myeclipse10下搭建SSH框架(图解)Struts2.1+Spring3.0+Hibernate3.3
- android dalvik-cache
- C++中函数指针的使用
- 高精度计数器
- 从浏览器输入回车到内容呈现的过程是怎样的?
- Java for-each循环使用难题2例(高级使用方法)