public static string HttpUploadFile_voice(string url, string path)
{
try
{
int index = path.LastIndexOf("\\");
string fileName = path.Substring(index + 1);
//文件
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
// FileStream fileStream= GetFileStream(path);
BinaryReader br = new BinaryReader(fileStream);
byte[] buffer = br.ReadBytes(Convert.ToInt32(fileStream.Length));
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
//请求
WebRequest req = WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "multipart/form-data; boundary=" + boundary;
//组织表单数据
StringBuilder sb = new StringBuilder();
sb.Append("--" + boundary + "\r\n");
sb.Append("Content-Disposition: form-data; name=\"media\"; filename=\"" + fileName + "\"; filelength=\"" + fileStream.Length + "\"");
sb.Append("\r\n");
sb.Append("Content-Type:application/octet-stream");
sb.Append("\r\n\r\n");
string head = sb.ToString();
byte[] form_data = Encoding.UTF8.GetBytes(head);
//结尾
byte[] foot_data = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n");
//post总长度
long length = form_data.Length + fileStream.Length + foot_data.Length;
req.ContentLength = length;
Stream requestStream = req.GetRequestStream();
requestStream.ReadTimeout = 3000;
//这里要注意一下发送顺序,先发送form_data > buffer > foot_data
//发送表单参数
requestStream.Write(form_data, 0, form_data.Length);
//发送文件内容
requestStream.Write(buffer, 0, buffer.Length);
//结尾
requestStream.Write(foot_data, 0, foot_data.Length);
requestStream.Close();
fileStream.Close();
fileStream.Dispose();
br.Close();
//br.Dispose();
req.Timeout = 3000;
string html = string.Empty;
//响应
using (WebResponse pos = req.GetResponse())
{
StreamReader sr = new StreamReader(pos.GetResponseStream(), Encoding.UTF8);
html = sr.ReadToEnd().Trim();
sr.Close();
sr.Dispose();
if (pos != null)
{
pos.Close();
}
}
if (req != null)
{
req = null;
}
return html;
}
catch
{ }
return "";
}
难道不能用插入代码形式插入代码么?