背景
我同事写了一个文件管理系统,可以上传下载附件,用户要求在邮件中可以下载当时上传的附件,我呢,就想偷个懒,不想自己写存放的地方,所以就打算调用她的接口,结果,半天搞不动,但是前端都能调用,简直是难过啊!好在,经过我不懈的努力终于还是调用成功了。
遇到的问题
首先就是这个http请求该怎么发送呢,好在以前写过,找到代码,只是没有上传附件的,改了N久,嗯,妥了!
- 【如何上传附件】这是我找到的第一个参考文献,其实也是最有用的,可惜刚看到的时候只觉得好,还是走了很多弯路,又看着它,才完成了。
- 报错一【 FileUploadException: the request was rejected because no multipart boundary was found】
- https://blog.csdn.net/whiteGu/article/details/89028674
- 原因:因为浏览器在发送表单的enctype =“multipart / form - data”,会在往后台发送请求的时候添加一个 【boundary】属性;而自己发送的ajax并没有携带这个参数,就导致后端在解析的时候,没找到这么属性。然后抛出异常。着重就是在修改这个地方,然后把文件流一起传过去(文件流没在这里体现)。
string boundary = "----WebKitFormBoundary" + DateTime.Now.Ticks.ToString("x"); //byte[] boundarybytes = Encoding.ASCII.GetBytes(boundary); byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); //if (contentType == "multipart/form-data") // contentType += ";boundary=" + boundary; request.ContentType = "multipart/form-data;boundary=" + boundary;
- 报错二【Could not set parameters for mapping】
- 原因:----WebKitFormBoundary我怀疑是我之前中划线(-)个数不对。调用成功,但是查询不到文件!!! 不是找不到,是我编码格式不一致,数据转字节一部分 ASCII,后来统一用UTF-8转byte,嘿嘿!
- 参考的是我们前端的调用,我就把我后台的拼接成这个样子的(开发者模式下是能够查看到的,快捷键F12是可以打开开发者模式的,只要没被禁用,或者快捷键冲突)
代码
文件转成流,存放于bytebuffer
filePath:文件的路径。上传你总得先获取到文件不是。
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
byte[] bytebuffer;
bytebuffer = new byte[fs.Length];
fs.Read(bytebuffer, 0, (int)fs.Length);
fs.Close();
我的文件是已经转成了流传进来的
public static string HttpPostForm(string url, Dictionary<string, string> postData = null, Dictionary<string, byte[]> files = null, int timeOut = 120, Dictionary<string, string> headers = null)
{
// HTTP请求 会携带很多头部数据,以及body数据。头部数据量小,body数据量大,附件这种一般都是放body里。
// 以字节流的形式放在stream里,随着请求一起发出去!!(理解不一定对,但大致应该是这样的),你给别人发消息
postData = postData ?? null;
ServicePointManager.DefaultConnectionLimit = 512;
request = WebRequest.Create(url) as HttpWebRequest;
string boundary = "----WebKitFormBoundary" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
byte[] endbytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
request.ContentType = "multipart/form-data;boundary=" + boundary;
request.Timeout = timeOut * 1000;
if (headers != null)
{
foreach (var header in headers)
request.Headers.Add(header.Key, header.Value);
}
request.Method = "POST";
string postdata = JsonConvert.SerializeObject(postData);
byte[] data = Encoding.UTF8.GetBytes(postdata);
using (Stream stream = request.GetRequestStream())
{
//stream.Write(data, 0, data.Length);
//1.1 key/value
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}";
foreach (var item in postData)
{
stream.Write(boundarybytes, 0, boundarybytes.Length);
string formitem = string.Format(formdataTemplate, item.Key, item.Value);
byte[] formitembytes = Encoding.UTF8.GetBytes(formitem);
stream.Write(formitembytes, 0, formitembytes.Length);
}
//1.2 file
if (files != null)
{
// 有附件则上传
foreach (var file in files)
{
stream.Write(boundarybytes, 0, boundarybytes.Length);
string temp = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\r\n\r\n";
string header = string.Format(temp, "file", file.Key);
byte[] fileName = Encoding.UTF8.GetBytes(header);
stream.Write(fileName, 0, fileName.Length);
stream.Write(file.Value);
}
}
//1.3 form end
stream.Write(endbytes, 0, endbytes.Length);
}
// HTTP响应,也就是你给别人发的消息,别人看到了,给你回消息
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
using (var httpStreamReader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
{
return httpStreamReader.ReadToEnd();
}
}
参考文章
the request was rejected because no multipart boundary was found