C#webapi⽂件流stream两种上传⽅式《第⼀部分⽂件流》
博客仅⽤于记录⼯作学习遇到的坑,欢迎交流!
1.⽂件流
1.1 客户端
电脑的ip地址怎么看从上传组件中获取InputStream,转换为byte[],组装对象上传
try{    byte[] buffer = new byte[filedata.InputStream.Length];
filedata.InputStream.Read(buffer, 0, buffer.Length);黄英多高
var requstData = new
卩字旁的字{
file = buffer,
path = resultPath,
老面怎么发酵fileName = filename
};
var result = UrlRequestHelper.HttpPostRequst(ConfigurationManager.AppSettings["FileUploadPath"], JsonConvert.SerializeObject(requstData), "");            }
httppost重写
/// <summary>
/// 发送post请求
/// </summary>
/// <param name="postData"></param>
/// <returns></returns>
public static string HttpPostRequst(string url, string postData, string ActionUrl)
{
byte[] bs = Encoding.UTF8.GetBytes(postData);
HttpWebRequest result = (HttpWebRequest)WebRequest.Create(url + ActionUrl);
result.ContentType = "application/json";
result.ContentLength = bs.Length;
result.Method = "POST";
using (Stream reqStream = result.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length);
}
using (WebResponse wr = result.GetResponse())
{
string reader = new StreamReader(wr.GetResponseStream(),
Encoding.UTF8).ReadToEnd();
return reader;
}
}
设置webconfig的maxRequestLength
<!--maxRequestLength就是⽂件的最⼤字符数,最⼤值不能超过2个G左右,executionTimeout是超时时间-->
<httpRuntime targetFramework="4.5" maxRequestLength="1073741824" executionTimeout="3600" requestValidationMode="2.0" />
中国女装品牌有哪些1.2 webapi
配置接收最⼤值
<!--配置允许接收最⼤G -->
<requestFiltering>
<requestLimits maxAllowedContentLength="2000000000" />
</requestFiltering>
</security>
由[FromBody] object bit 接收,DeserializeObject转对象
[HttpPost]
public string FileUp([FromBody] object bit)
{
lock (_lock)
{
string jsonstr = JsonConvert.SerializeObject(bit);
Data res = JsonConvert.DeserializeObject<Data>(jsonstr);
return SaveFile(res.file, res.path, res.fileName);
}
}
重点:byte[] 转stream  ,写⼊⽂件;
private string SaveFile(byte[] file, string path, string fileName)
{
try
{
var serverPath = configPath;  //存储路径
CreatePath(serverPath + @path);
var filename = fileName;
var filePath = string.Format(@"{0}\{1}", serverPath + @path, filename);    //保存完整路径                var fullpath = filePath;    //流写⼊路径
//创建⽂件
using (var ms = new MemoryStream())
{
MemoryStream m = new MemoryStream(file);
string files = string.Format(@"{0}", fullpath);
FileStream fs = new FileStream(files, FileMode.OpenOrCreate);
m.WriteTo(fs);
m.Close();
fs.Close();
m = null;
fs = null;
return fullpath;
}
}
catch (Exception ex)
{
throw ex;
}
}
遇到的坑:⽂件夹权限问题(如果没遇到可以不⽤)
/// 创建⽂件路径
/// </summary>
/// <param name="filepath"></param>
/// <returns></returns>
private void CreatePath(string filepath)
博客上传视频
{
if (!Directory.Exists(filepath))
{
var securityRules = new DirectorySecurity();
securityRules.AddAccessRule(new FileSystemAccessRule(System.Environment.UserName, FileSys
temRights.FullControl, AccessControlType.Allow));                securityRules.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
securityRules.AddAccessRule(new FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.FullControl, AccessControlType.Allow));
Directory.CreateDirectory(filepath, securityRules);
}
}
学习笔记!