c# 后台模拟表单post 附带参数上传文件

?^man^ 2016-10-09 06:15:39
DirectoryInfo TheFolder = new DirectoryInfo(filePath);
string fileNamePath = "";
string fileNameExt = "";
string sGuid = "";
string saveName = "";
foreach (FileInfo NextFile in TheFolder.GetFiles())
{
fileNamePath = "";
fileNameExt = "";
sGuid = "";
saveName = "";

fileNamePath = filePath + "\\" + NextFile.Name;
fileNameExt = fileNamePath.Substring(+fileNamePath.LastIndexOf(".") + 1);
sGuid = System.Guid.NewGuid().ToString() + "." + fileNameExt;
//saveName = sGuid;
//saveName = NextFile.Name.Substring(0, NextFile.Name.LastIndexOf(".")) + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + fileNameExt;
saveName = NextFile.Name.Substring(0, NextFile.Name.LastIndexOf(".")) + "." + fileNameExt;
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(fs); //时间戳
string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "--\r\n"); //请求头部信息
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(strBoundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append("Filedata");
sb.Append("\"; filename=\"");
sb.Append(saveName);
sb.Append("\";");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append("application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");

sb.Append("Content-Disposition: form-data; userkey=\"");
sb.Append(key);
sb.Append("\r\n");
sb.Append("\r\n");

//sb.Append("--");
//sb.Append(strBoundary);
//sb.Append("\r\n");
//sb.Append("Content-Disposition: form-data; name=\"");
//sb.Append(key);
//sb.Append("\r\n");
//sb.Append("\r\n");
string strPostHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(strPostHeader); // 根据uri创建HttpWebRequest对象
HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(new Uri(address));
httpReq.Method = "POST"; //对发送的数据不使用缓存
//httpReq.AllowWriteStreamBuffering = false; //设置获得响应的超时时间(300秒)
httpReq.Timeout = 30000000;
httpReq.ContentType = "multipart/form-data; boundary=" + strBoundary;
long length = fs.Length + postHeaderBytes.Length + boundaryBytes.Length;
long fileLength = fs.Length;
httpReq.ContentLength = length;
try
{
progressBar.Maximum = int.MaxValue;
progressBar.Minimum = 0;
progressBar.Value = 0;
int bufferLength = 4096;
byte[] buffer = new byte[bufferLength];
//已上传的字节数
long offset = 0;
//开始上传时间
DateTime startTime = DateTime.Now;
int size = r.Read(buffer, 0, bufferLength);
Stream postStream = httpReq.GetRequestStream();
//发送请求头部消息
postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
while (size > 0)
{
postStream.Write(buffer, 0, size);
offset += size;
progressBar.Value = (int)(offset * (int.MaxValue / length));
TimeSpan span = DateTime.Now - startTime;
double second = span.TotalSeconds;
label2.Text = "已上传:" + (offset * 100.0 / length).ToString("F2") + "%";
Application.DoEvents();
size = r.Read(buffer, 0, bufferLength);
if (size==0)
{
label2.Text = "已上传:100%";
progressBar.Value = int.MaxValue;
}
}
//添加尾部的时间戳
postStream.Write(boundaryBytes, 0, boundaryBytes.Length);
postStream.Close(); //获取服务器端的响应
WebResponse webRespon = httpReq.GetResponse();
Stream s = webRespon.GetResponseStream();
//读取服务器端返回的消息
StreamReader sr = new StreamReader(s);
String sReturnString = sr.ReadLine();
s.Close();
sr.Close();
JObject jo1 = (JObject)JsonConvert.DeserializeObject(sReturnString);
string resultcode = jo1["resultcode"].ToString();
string description = jo1["description"].ToString();
if (resultcode == "200" && description == "成功!")
{
returnValue = 1;
progressBar.Value = int.MaxValue;
}
else
{
returnValue = 0;
}
}
catch (Exception e)
{
label2.Text = "上传失败";
throw e;

//returnValue = 0;
}
finally
{
fs.Close();
r.Close();
}


sb.Append("Content-Disposition: form-data; userkey=\"");
sb.Append(key);
sb.Append("\r\n");
sb.Append("\r\n");
这一段是我新添加的,标识新增一个参数,但是无法上传,提示500,应该是参数问题
...全文
342 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
?^man^ 2016-10-10
  • 打赏
  • 举报
回复
引用 1 楼 cx067261 的回复:

sb.Append("Content-Disposition: form-data; userkey=\"");
sb.Append(key);
sb.Append("\r\n");
sb.Append("\r\n");
userkey没有写结束的双引号?加上试试?
解决了,是少半个引号,但是我改了写法
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
                BinaryReader r = new BinaryReader(fs);     //时间戳 
                string strBoundary = "----------" + DateTime.Now.Ticks.ToString("x");
                byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + strBoundary + "--\r\n");     //请求头部信息 
                StringBuilder sb = new StringBuilder();
                sb.Append("--");
                sb.Append(strBoundary);
                sb.Append("\r\n");

                sb.Append("Content-Disposition: form-data; name=\"");
                sb.Append("userkey\"");
                sb.Append("\r\n");
                sb.Append("\r\n");
                sb.Append(key);
                sb.Append("\r\n");


                sb.Append("--");
                sb.Append(strBoundary);
                sb.Append("\r\n");
                sb.Append("Content-Disposition: form-data; name=\"");
                sb.Append("Filedata");
                sb.Append("\"; filename=\"");
                sb.Append(saveName);
                sb.Append("\";");
                sb.Append("\r\n");
                sb.Append("Content-Type: ");
                sb.Append("application/octet-stream");
                sb.Append("\r\n");
                sb.Append("\r\n");
?^man^ 2016-10-10
  • 打赏
  • 举报
回复
引用 1 楼 cx067261 的回复:

sb.Append("Content-Disposition: form-data; userkey=\"");
sb.Append(key);
sb.Append("\r\n");
sb.Append("\r\n");
userkey没有写结束的双引号?加上试试?
结束的双引号???我没明白
?^man^ 2016-10-10
  • 打赏
  • 举报
回复
引用 1 楼 cx067261 的回复:

sb.Append("Content-Disposition: form-data; userkey=\"");
sb.Append(key);
sb.Append("\r\n");
sb.Append("\r\n");
userkey没有写结束的双引号?加上试试?
引用 1 楼 cx067261 的回复:

sb.Append("Content-Disposition: form-data; userkey=\"");
sb.Append(key);
sb.Append("\r\n");
sb.Append("\r\n");
userkey没有写结束的双引号?加上试试?
sb.Append("Content-Disposition: form-data; userkey=\""); sb.Append(key); sb.Append("\";"); sb.Append("\r\n"); sb.Append("\r\n"); 是不是这样???、
陈悕 2016-10-09
  • 打赏
  • 举报
回复

sb.Append("Content-Disposition: form-data; userkey=\"");
sb.Append(key);
sb.Append("\r\n");
sb.Append("\r\n");
userkey没有写结束的双引号?加上试试?

110,552

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧