首页 新闻 会员 周边 捐助

c#语音素材上传为甚么一直报47001错误

0
悬赏园豆:15 [已关闭问题] 关闭于 2017-05-10 10:10

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 "";
}

笑叹、的主页 笑叹、 | 初学一级 | 园豆:34
提问于:2016-09-22 18:12
< >
分享
所有回答(1)
0

难道不能用插入代码形式插入代码么?

顾晓北 | 园豆:10898 (专家六级) | 2016-09-23 09:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册