string url = string.Format("{0}/api/FileUpload/xxx", "http://192.168.1.1:80xx");
Dictionary<string, object> parameters = new Dictionary<string, object>();
List<string> files = new List<string>();
files.Add($"{path}\\{fName}");
parameters.Add("files", files);
parameters.Add("ter", ster);
parameters.Add("img", "");
public const string Token = "U0pfVG9rZW5fMjRiZGViYjNmZmRlNGYxOGI0MTZkNzhkMTFjNDlmZWU="; //测试token
public static HttpWebResponse PostFile(string url, IDictionary<string, object> kys = null, string postdata = null)
{
//即将上传的文件根路径
string filepath = Path.Combine(AppContext.BaseDirectory, "Screen");
Dictionary<string, object> files = new Dictionary<string, object>();
foreach (var item in kys)
{
if (item.Key=="files")
{
List<string> jsonFile = JsonConvert.DeserializeObject<List<string>>(JsonConvert.SerializeObject(item.Value));
foreach (var item2 in jsonFile)
{
files.Add(Path.GetFileName(item2), item2);
}
}
}
//上传接口地址
var webRequest = HttpWebRequest.Create(url);
webRequest.Method = "POST"; //POST提交方式
webRequest.Timeout = 60000; //请求超时时间
webRequest.Headers.Add("token", Token); // ** token 必填
// 边界符 定义
var boundary = "------WebKitFormBoundary" + DateTime.Now.Ticks.ToString("x");
webRequest.ContentType = "multipart/form-data; boundary=" + boundary; //form-data 形式的请求头类型
//用于打印的可以忽略
string start = "--" + boundary + "\r\n";
string end = "--" + boundary + "--\r\n";
string newline = "\r\n";
//写入流的固定格式值
var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n"); // 开始边界符
var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n"); // 结束结束符
var newLineBytes = Encoding.UTF8.GetBytes("\r\n"); //换行符
//区分上传类型 获得 content 类型
Func<string, string> getcontent = (s) =>
{
var ex = Path.GetExtension(s); //拿到后缀
return FileContentType.GetMimeType(ex);//FileContentType.GetMimeType(ex); //请求后缀返回对应的 type类型
//PS 获取Content-Type 的 三种方法传送门:https://blog.csdn.net/a873744779/article/details/100514010
};
using (var stream = new MemoryStream())
{
// 写入字段参数
var keyValue = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
// 装载form表单字段【非上传文件字段】
foreach (string key in kys.Keys)
{
//参数字段转字节
var keyValueBytes = Encoding.UTF8.GetBytes(string.Format(keyValue, key, kys[key]));
stream.Write(beginBoundary, 0, beginBoundary.Length);//写入边界开始
stream.Write(keyValueBytes, 0, keyValueBytes.Length);//写入字节
stream.Write(newLineBytes, 0, newLineBytes.Length);//写入换行符
}
//多文件上传
foreach (var item in files)
{
var fileData = File.ReadAllBytes(item.Value.ToString()); //读文件流
// 写入文件 name = \"Files\" 这里对应的是接口参数 List<IfromFile> Files
var fileHeader = "Content-Disposition: form-data; name=\"Files\"; filename=\"" + item.Key + "\"\r\n"
+ "Content-Type: " + getcontent(item.Key) + "\r\n\r\n";
var fileHeaderBytes = Encoding.UTF8.GetBytes(fileHeader);
stream.Write(beginBoundary, 0, beginBoundary.Length);// 写入开始边界
stream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);//写入文件格式对象流
stream.Write(fileData, 0, fileData.Length); // 写入文件流对象
stream.Write(newLineBytes, 0, newLineBytes.Length);//写入换行符
}
Console.WriteLine(end);//打印结束边界
// 写入结束边界符
stream.Write(endBoundary, 0, endBoundary.Length);
webRequest.ContentLength = stream.Length;//设置请求对象,字节长度总量
stream.Position = 0;//指定流开始索引
var tempBuffer = new byte[stream.Length];//定义新字节流对象,用于提交请求结果
stream.Read(tempBuffer, 0, tempBuffer.Length); //从0开始索引读流到新 tempbuffer 对象
//请求结果流
using (Stream requestStream = webRequest.GetRequestStream())
{
requestStream.Write(tempBuffer, 0, tempBuffer.Length);//写入请求流
using (var response = webRequest.GetResponse())//请求结果流
using (StreamReader httpStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) //从结果流对象中读取结果流并设置流格式转换为 uft8 格式
{
string result = httpStreamReader.ReadToEnd();//返回服务器返回json
}
}
}
return null;
}
方法
[HttpPost]
public IActionResult TerScreenshotUpload(List<IFormFile> files, string ter, string img)
{
}
网友评论