ASP.NET服务端接收Multipartform-data文件异步

ASP.NET服务端接收Multipartform-data文件异步

2023年7月14日发(作者:)

服务端接收Multipartform-data⽂件异步 服务端接收Multipart/form-data⽂件在⽹络编程过程中需要向服务器上传⽂件。Multipart/form-data是上传⽂件的⼀种⽅式。 1 ///

2 /// 上传⼯程⽂件 3 /// 4 /// 5 public async Task UploadProjectFile() 6 { 7 ProjectFile postData = new ProjectFile(); 8 IEnumerable stringValues; 9 //获取请求头⽂件相关参数 10 Values("token", out stringValues); 11 = rDefault(); 12

13 Values("uid", out stringValues); 14 = (rDefault()); 15

16 Values("project_id", out stringValues); 17 t_id = (rDefault()); 18

19 Values("md5", out stringValues); 20 5 = rDefault(); 21

22 Values("file_name", out stringValues); 23 _name = rDefault(); 24 //验证Token 25 var result = CheckToken(); 26 if (!result) 27 { 28 return CreateResponseError(401, "请求拒绝"); 29 } 30 //获取⽂件名,这⾥的⽂件名必须带扩展名 31 string fileName = _name; 32

33 int projectId = t_id; 34 //获取应⽤程序的当前⼯作⽬录 35 string rootPath = h("~/"); 36 //通过Path类的Combine⽅法可以合并路径。获取当前⼯程⽂件夹地址 37 string directoryPath = e(rootPath, "BgerProject", ng()); 38 //创建⽬录时如果⽬录已存在,则不会重新创建⽬录,且不会报错。创建⽬录时会⾃动创建路径中各级不存在的⽬录。 39 Directory(directoryPath); 40 //获取当前⼯程⽂件地址 41 string filePath = e(directoryPath, fileName); 42 if (MultipartContent()) 43 { 44 Dictionary dic = new Dictionary(); 45 var provider = new MultipartFormDataMemoryStreamProvider(); 46

47 await MultipartAsync(provider); 48 foreach (var item in ntents) 49 { 50 //ine(me);//获取上传⽂件实际的⽂件名

51 //ine("Server file path: " + ileName);//获取上传⽂件在服务上默认的⽂件名

52 var stream = await StreamAsync(); 53 if (ension(e(""", "")) == ".mp4") 54 { 55 //通过正则表达式判断是视频还是⾳频 56 Regex regex = new Regex("voice", ed); 57 Match m = (r()); 58 if (s) 59 { 60 //⾳频直接保存处理 61

62 //判断是否已存在该⾳频 63 if (!(filePath)) 64 { 65 using (StreamWriter sw = new StreamWriter(filePath)) 66 { 67 (ream); 68 (); 69 } 70 } 71 } 72 else 73 { 74 //视频进⾏压缩处理 75 //保存原⽚(如果路径不存在,创建路径) 76

77 using (StreamWriter sw = new StreamWriter(filePath)) 78 { 79 (ream); 80 (); 81 } 82 //压缩并保存上传的视频 83 //压缩后的存放路径(如果路径不存在,创建路径) 84 ////判断是否已存在该视频 85 //if (!(filePath)) 86 //{ 87 // //压缩保存视频 88 // if (!NewVideo(originalFilePath, filePath)) 89 // { 90 // continue; 91 // }; 92 //} 93 } 94 } 95 else 96 { 97 //保存原⽚(如果路径不存在,创建路径) 98

99 using (StreamWriter sw = new StreamWriter(filePath))100 {101 (ream);102 ();103 }104 //压缩并保存上传的图⽚105 //压缩后的存放路径(如果路径不存在,创建路径)106

107 ////判断是否已存在该图⽚108 //if (!(filePath))109 //{110 // var pxLimit = 481;111 // //判断是否需要压缩112 // iSource = le(originalFilePath);113 // if ( <= pxLimit || <= pxLimit)114 // {115 // //⽆需压缩116 // (originalFilePath, filePath);117 // }118 // else119 // {120 // var min = ((int), (int));121 // if (min > pxLimit)122 // {123 // //缩放倍率124 // var rate = (double)pxLimit / min;125 // var width = (int)g(rate * (double));126 // var height = (int)g(rate * (double));127

128 // //压缩保存图⽚129 // if (!Thumbnail(originalFilePath, filePath, height, width, 100))130 // {131 // continue;132 // };133 // }134 // }135 //}136 }137 }138 return CreateResponse("请求成功");139

140 }141 return CreateResponseError(202,"缺少MIME内容");142

143 } 1 using System; 2 using c; 3 using ; 4 using ; 5 using Model; 6 using lized; 7 using ; 8 using ; 9 using s;10 using ing;11 using ;12

13 namespace 14 {15 ///

16 /// 与MultipartFormDataStreamProvider对应,但不将⽂件直接存⼊指定位置,⽽是需要⾃⼰指定数据流如何保存

17 ///

18 public class MultipartFormDataMemoryStreamProvider : MultipartStreamProvider19 {20 private NameValueCollection _formData = new NameValueCollection();21 private Collection _isFormData = new Collection();22 ///

23 /// 获取⽂件对应的HttpContent集合,⽂件如何读取由实际使⽤⽅确定,可以ReadAsByteArrayAsync,也可以ReadAsStreamAsync

24 ///

25 public Collection FileContents26 {27 get28 {29 if (this._ != )//两者总数不⼀致,认为未执⾏过必须的MultipartAsync(provider)⽅法

30 {31 throw new InvalidOperationException("MultipartAsync must be called first!");32 }33 return new Collection(((ct, idx) => !this._isFormData[idx]).ToList());34 }35 }36 ///

Gets a of form data passed as part of the multipart form data.

37 /// The of form data.

38 public NameValueCollection FormData39 {40 get41 {42 return this._formData;43 }44 }45 public override async Task ExecutePostProcessingAsync()46 {47 for (var i = 0; i < ; i++)48 {49 if (!this._isFormData[i])//⾮⽂件

50 {51 continue;52 }53 var formContent = ts[i];54 ContentDispositionHeaderValue contentDisposition = tDisposition;55 string formFieldName = UnquoteToken() ?? ;56 string formFieldValue = await StringAsync();57 (formFieldName, formFieldValue);58 }59 }60 public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)61 {62 if (parent == null)63 {64 throw new ArgumentNullException("parent");65 }66 if (headers == null)67 {68 throw new ArgumentNullException("headers");69 }70 ContentDispositionHeaderValue contentDisposition = tDisposition;71 if (contentDisposition == null)72 {73 throw new InvalidOperationException("Content-Disposition is null");74 }75 this._(OrEmpty(me));76 return new MemoryStream();77 }78 ///

79 /// 复制⾃ tingUtilities 下同名⽅法,因为该类为internal,不能在其它命名空间下被调⽤

80 ///

81 ///

82 ///

83 private static string UnquoteToken(string token)84 {85 if (OrWhiteSpace(token))86 {87 return token;88 }89 if (With(""", l) && th(""", l) && > 1)90 {91 return ing(1, - 2);92 }93 return token;94 }95 }96 }

发布者:admin,转转请注明出处:http://www.yc00.com/news/1689306722a228364.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信