110,552
社区成员
发帖
与我相关
我的任务
分享
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();
}
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");
sb.Append("Content-Disposition: form-data; userkey=\"");
sb.Append(key);
sb.Append("\r\n");
sb.Append("\r\n");
userkey没有写结束的双引号?加上试试?