ASP.NET 服务端接收Multipart/form-data文件
在网络编程过程中需要向服务器上传文件。
Multipart/form-data是上传文件的一种方式。
/// <summary>
/// 上传工程文件
/// </summary>
/// <returns></returns>
public async Task<HttpResponseMessage> UploadProjectFile()
{
ProjectFile postData = new ProjectFile();
IEnumerable<string> stringValues;
//获取请求头文件相关参数
Request.Headers.TryGetValues("token", out stringValues);
postData.token = stringValues.FirstOrDefault(); Request.Headers.TryGetValues("uid", out stringValues);
postData.uid = int.Parse(stringValues.FirstOrDefault()); Request.Headers.TryGetValues("project_id", out stringValues);
postData.project_id = int.Parse(stringValues.FirstOrDefault()); Request.Headers.TryGetValues("md5", out stringValues);
postData.md5 = stringValues.FirstOrDefault(); Request.Headers.TryGetValues("file_name", out stringValues);
postData.file_name = stringValues.FirstOrDefault();
//验证Token
var result = CheckToken(postData.token);
if (!result)
{
return CreateResponseError(, "请求拒绝");
}
//获取文件名,这里的文件名必须带扩展名
string fileName = postData.file_name; int projectId = postData.project_id;
//获取应用程序的当前工作目录
string rootPath = HostingEnvironment.MapPath("~/");
//通过Path类的Combine方法可以合并路径。获取当前工程文件夹地址
string directoryPath = Path.Combine(rootPath, "BgerProject", projectId.ToString());
//创建目录时如果目录已存在,则不会重新创建目录,且不会报错。创建目录时会自动创建路径中各级不存在的目录。
Directory.CreateDirectory(directoryPath);
//获取当前工程文件地址
string filePath = Path.Combine(directoryPath, fileName);
if (Request.Content.IsMimeMultipartContent())
{
Dictionary<string, string> dic = new Dictionary<string, string>();
var provider = new MultipartFormDataMemoryStreamProvider(); await Request.Content.ReadAsMultipartAsync(provider);
foreach (var item in provider.FileContents)
{
//Trace.WriteLine(item.Headers.ContentDisposition.FileName);//获取上传文件实际的文件名
//Trace.WriteLine("Server file path: " + item.LocalFileName);//获取上传文件在服务上默认的文件名
var stream = await item.ReadAsStreamAsync();
if (Path.GetExtension(item.Headers.ContentDisposition.FileName.Replace("\"", "")) == ".mp4")
{
//通过正则表达式判断是视频还是音频
Regex regex = new Regex("voice", RegexOptions.Compiled);
Match m = regex.Match(item.Headers.ContentDisposition.Name.ToLower());
if (m.Success)
{
//音频直接保存处理 //判断是否已存在该音频
if (!File.Exists(filePath))
{
using (StreamWriter sw = new StreamWriter(filePath))
{
stream.CopyTo(sw.BaseStream);
sw.Flush();
}
}
}
else
{
//视频进行压缩处理
//保存原片(如果路径不存在,创建路径) using (StreamWriter sw = new StreamWriter(filePath))
{
stream.CopyTo(sw.BaseStream);
sw.Flush();
}
//压缩并保存上传的视频
//压缩后的存放路径(如果路径不存在,创建路径)
////判断是否已存在该视频
//if (!File.Exists(filePath))
//{
// //压缩保存视频
// if (!FFMPEGHelper.CreateNewVideo(originalFilePath, filePath))
// {
// continue;
// };
//}
}
}
else
{
//保存原片(如果路径不存在,创建路径) using (StreamWriter sw = new StreamWriter(filePath))
{
stream.CopyTo(sw.BaseStream);
sw.Flush();
}
//压缩并保存上传的图片
//压缩后的存放路径(如果路径不存在,创建路径) ////判断是否已存在该图片
//if (!File.Exists(filePath))
//{
// var pxLimit = 481;
// //判断是否需要压缩
// System.Drawing.Image iSource = System.Drawing.Image.FromFile(originalFilePath);
// if (iSource.Width <= pxLimit || iSource.Height <= pxLimit)
// {
// //无需压缩
// System.IO.File.Copy(originalFilePath, filePath);
// }
// else
// {
// var min = Math.Min((int)iSource.Width, (int)iSource.Height);
// if (min > pxLimit)
// {
// //缩放倍率
// var rate = (double)pxLimit / min;
// var width = (int)Math.Ceiling(rate * (double)iSource.Width);
// var height = (int)Math.Ceiling(rate * (double)iSource.Height); // //压缩保存图片
// if (!ImageHelper.GetPicThumbnail(originalFilePath, filePath, height, width, 100))
// {
// continue;
// };
// }
// }
//}
}
}
return CreateResponse("请求成功"); }
return CreateResponseError(,"缺少MIME内容"); }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading;
using System.Threading.Tasks; namespace BgerServer.Models
{
/// <summary>
/// 与MultipartFormDataStreamProvider对应,但不将文件直接存入指定位置,而是需要自己指定数据流如何保存
/// </summary>
public class MultipartFormDataMemoryStreamProvider : MultipartStreamProvider
{
private NameValueCollection _formData = new NameValueCollection();
private Collection<bool> _isFormData = new Collection<bool>();
/// <summary>
/// 获取文件对应的HttpContent集合,文件如何读取由实际使用方确定,可以ReadAsByteArrayAsync,也可以ReadAsStreamAsync
/// </summary>
public Collection<HttpContent> FileContents
{
get
{
if (this._isFormData.Count != this.Contents.Count)//两者总数不一致,认为未执行过必须的Request.Content.ReadAsMultipartAsync(provider)方法
{
throw new InvalidOperationException("System.Net.Http.HttpContentMultipartExtensions.ReadAsMultipartAsync must be called first!");
}
return new Collection<HttpContent>(this.Contents.Where((ct, idx) => !this._isFormData[idx]).ToList());
}
}
/// <summary>Gets a <see cref="T:System.Collections.Specialized.NameValueCollection" /> of form data passed as part of the multipart form data.</summary>
/// <returns>The <see cref="T:System.Collections.Specialized.NameValueCollection" /> of form data.</returns>
public NameValueCollection FormData
{
get
{
return this._formData;
}
}
public override async Task ExecutePostProcessingAsync()
{
for (var i = ; i < this.Contents.Count; i++)
{
if (!this._isFormData[i])//非文件
{
continue;
}
var formContent = this.Contents[i];
ContentDispositionHeaderValue contentDisposition = formContent.Headers.ContentDisposition;
string formFieldName = UnquoteToken(contentDisposition.Name) ?? string.Empty;
string formFieldValue = await formContent.ReadAsStringAsync();
this.FormData.Add(formFieldName, formFieldValue);
}
}
public override Stream GetStream(HttpContent parent, HttpContentHeaders headers)
{
if (parent == null)
{
throw new ArgumentNullException("parent");
}
if (headers == null)
{
throw new ArgumentNullException("headers");
}
ContentDispositionHeaderValue contentDisposition = headers.ContentDisposition;
if (contentDisposition == null)
{
throw new InvalidOperationException("Content-Disposition is null");
}
this._isFormData.Add(string.IsNullOrEmpty(contentDisposition.FileName));
return new MemoryStream();
}
/// <summary>
/// 复制自 System.Net.Http.FormattingUtilities 下同名方法,因为该类为internal,不能在其它命名空间下被调用
/// </summary>
/// <param name="token"></param>
/// <returns></returns>
private static string UnquoteToken(string token)
{
if (string.IsNullOrWhiteSpace(token))
{
return token;
}
if (token.StartsWith("\"", StringComparison.Ordinal) && token.EndsWith("\"", StringComparison.Ordinal) && token.Length > )
{
return token.Substring(, token.Length - );
}
return token;
}
}
}
ASP.NET 服务端接收Multipart/form-data文件的更多相关文章
- Asp.Net_ 服务端向客户端写JavaScript脚本
在Asp.net 服务端处理脚本,一般都用 ClientScriptManager ,即web窗体服务端的this.ClientScript.该对象比较常用的方法: 1.RegisterArrayDe ...
- C# TCP socket发送大数据包时,接收端和发送端数据不一致 服务端接收Receive不完全
简单的c# TCP通讯(TcpListener) C# 的TCP Socket (同步方式) C# 的TCP Socket (异步方式) C# 的tcp Socket设置自定义超时时间 C# TCP ...
- 自己实现FormsAuthentication.SetAuthCookie方法,怎样在ASP.NET服务端代码中删除客户端Cookie
如何手动设置AuthCookie ASP.NET中实现可以自己实现FormsAuthentication.SetAuthCookie方法,控制更为灵活 /// <summary> /// ...
- android如何与asp.net服务端共享session
近期需要实现一个功能,就是需要通过发送短信进行注册,现在想把短信验证码放到服务器的session值中,当客户端收到短信并提交短信码时由asp.net服务端进行判断,那么如何共享这个session那么需 ...
- MVC文件上传04-使用客户端jQuery-File-Upload插件和服务端Backload组件实现多文件异步上传
本篇使用客户端jQuery-File-Upload插件和服务端Badkload组件实现多文件异步上传.MVC文件上传相关兄弟篇: MVC文件上传01-使用jquery异步上传并客户端验证类型和大小 ...
- 微信小程序开发之多图片上传+服务端接收
前言: 业务需求,这次需要做一个小程序同时选中三张图片一起上传到服务端,后端使用的.NET WEBAPI接收数据保存. 使用技术: 在这章中将会使用到微信小程序wx.uploadFile(Object ...
- java httpclient发送json 请求 ,go服务端接收
/***java客户端发送http请求*/package com.xx.httptest; /** * Created by yq on 16/6/27. */ import java.io.IOEx ...
- [转]微信小程序开发(二)图片上传+服务端接收
本文转自:http://blog.csdn.net/sk719887916/article/details/54312573 文/YXJ 地址:http://blog.csdn.net/sk71988 ...
- tcp syn-synack-ack 服务端接收ack
TCP 服务端 接收到ack tcp_v4_rcv() -> tcp_v4_do_rcv() -> tcp_v4_hnd_req() + tcp_child_process()tcp_v4 ...
随机推荐
- Spring学习之旅(五)--AOP
什么是 AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是 OOP(Object-Oriented Programing,面向对象编程)的补充和完善. OO ...
- urllib爬虫模块
网络爬虫也称为网络蜘蛛.网络机器人,抓取网络的数据.其实就是用Python程序模仿人点击浏览器并访问网站,而且模仿的越逼真越好.一般爬取数据的目的主要是用来做数据分析,或者公司项目做数据测试,公司业务 ...
- ACM-ICPC 2018 徐州赛区(网络赛)
目录 A. Hard to prepare B.BE, GE or NE F.Features Track G.Trace H.Ryuji doesn't want to study I.Charac ...
- UOJ 34 多项式乘法 FFT 模板
这是一道模板题. 给你两个多项式,请输出乘起来后的多项式. 输入格式 第一行两个整数 nn 和 mm,分别表示两个多项式的次数. 第二行 n+1n+1 个整数,表示第一个多项式的 00 到 nn 次项 ...
- 杭电多校第九场 hdu6424 Rikka with Time Complexity 数学
Rikka with Time Complexity Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K ( ...
- CF - 1108 E 枚举上界+线段树维护
题目传送门 枚举每个点作为最大值的那个点.然后既然是作为最大值出现的话,那么这个点就是不需要被减去的,因为如果最小值也在这个区间内的话,2者都减去1,对答案没有影响,如果是最小值不出现在这个区间内的话 ...
- Atcoder F - Mirrored(思维+搜索)
题目链接:http://arc075.contest.atcoder.jp/tasks/arc075_d 题意:求rev(N)=N+D的个数,rev表示取反.例如rev(123)=321 题解:具体看 ...
- hdu 4734 F(x)(数位dp+优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4734 题意:我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2 ...
- hdu 1671 Phone List 字典树模板
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...
- CentOS7 中防火墙配置
systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall开机启动 开 ...