|
上传多媒体文件,一直返回{"errcode":41005,"errmsg":"media data missing"}
API文档说明里面采用的是:
curl -F media=@test.jpg "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE目测好像是php的实现方法
本人是使用C#,我正常使用POST方式,将文件POST到接口地址,一直返回:
{“errcode”:41005,”errmsg”:”media data missing”}
请教是否有C#的实现示例?
有关media的参数说明也有点没看懂:
form-data中媒体文件标识,有filename、filelength、content-type等信息
我理解的是在POST的时候,在HEARDER头里面加入这些标识信息,但是一直没有成功
另外,使用最基本的FORM表单方式,也是返回: {“errcode”:41005,”errmsg”:”media data missing”}
表单如下:
<form method="post" enctype="multipart/form-data" action="http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=31XZoFdHr7fCBJOSUskpR6Csl-NvyZK2S8TsERowO4mIHqEyjFo3JWutmKDnFa3eaGfE0_P0wBnbi2J2eQ0AgxMprjJEuFA8YZ5fRk6c5ASuPnrerErUzrRFGwfxIJQm3APkHDn-6p4AaIg7agq9Dg&type=image" method="post"><input type="file" /><input type="submit" value="提交"/></form>还请管理员指导,谢谢!
根据返回的代码,41005,缺少媒体文件。就是说你没有选择要上传服务器的文件! 望采纳!
2013年 11月 7日 作者: 社区管家
不能回答已答过的问题、已关闭的问题和你自己的提问
3个回答
+4投票
朋友,同样的问题,不知道你的解决了吗?
已回复 2013年 11月 7日 作者: 深圳市酷动数码 (300 积分)
我遇到同样的问题,使用WebClient 实现后可以了,下面贴上代码给你们
WebClient c = new WebClient();
string img = @”E:\MyWork\WeixinApi\WindowsFormsApplication1\WindowsFormsApplication1\images\8_u4a61HINaxpnZnlQmdy1ClKH1i23hcxgZGGFPdlyd-A9ewzxz5i0pZ7NBqIWuQ.jpg”;
byte[] result = c.UploadFile(new Uri(String.Format(“http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}“, token, “image”)), img);
string ll = Encoding.Default.GetString(result);
2013年 11月 20日 作者: 排队美食
+2投票
WebClient c = new WebClient();string img = @"E:\MyWork\WeixinApi\WindowsFormsApplication1\WindowsFormsApplication1\images\8_u4a61HINaxpnZnlQmdy1ClKH1i23hcxgZGGFPdlyd-A9ewzxz5i0pZ7NBqIWuQ.jpg";byte[] result = c.UploadFile(new Uri(String.Format("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}", token, "image")), img);string ll = Encoding.Default.GetString(result);已回复 2013年 11月 20日 作者: 排队美食 (190 积分)
难道只能通过WebClient 的方式进行上传多媒体文件么??
2013年 12月 22日 作者: 劲风工作室
我也很想知道答案,遇到同样的问题,用WebClient是可以的,为什么使用HttpWebRequest却不能上传
1月 2日 作者: 上海本丰
<input type=”file” name=”media” />加一个name=“media”
1月 2日 作者: 上海本丰
0投票
今天做项目的时候也遇到同样的问题,现在已经解决了,贴出来分享一下(本人是学习.net的)。两种解决方案:
第一,WebClient类去实现,代码如下:
WebClient c = new WebClient();
string filename = @”文件路径”;
byte[] result = c.UploadFile(new Uri(String.Format(“http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={0}&type={1}“, token, “image”)), filename);
string resultjson = Encoding.Default.GetString(result);
第二,WebRequest类去实现,代码如下:
/// <summary>
/// 服务号:上传多媒体文件
/// </summary>
/// <param name=”accesstoken”>调用接口凭据</param>
/// <param name=”type”>图片(image)、语音(voice)、视频(video)和缩略图(thumb)</param>
/// <param name=”filename”>文件路径</param>
/// <param name=”contenttype”>文件Content-Type类型(例如:image/jpeg、audio/mpeg)</param>
/// <returns></returns>
public string UploadFile(string accesstoken,string type,string filename,string contenttype)
{
//文件
FileStream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fileStream);
byte[] buffer = br.ReadBytes(Convert.ToInt32(fileStream.Length));
string boundary = “—————————” + DateTime.Now.Ticks.ToString(“x”);
//请求
WebRequest req = WebRequest.Create(@”http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=” + accesstoken + “&type=” + type);
req.Method = “POST”;
req.ContentType = “multipart/form-data; boundary=” + boundary;
已回复 1月 2日 作者: 上海本丰 (300 积分)
//组织表单数据
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: “+contenttype);
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();
//这里要注意一下发送顺序,先发送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();
//响应
WebResponse pos = req.GetResponse();
StreamReader sr = new StreamReader(pos.GetResponseStream(), Encoding.UTF8);
string html = sr.ReadToEnd().Trim();
sr.Close();
sr.Dispose();
if (pos != null)
{
pos.Close();
pos = null;
}
if (req != null)
{
req = null;
}
return html;
}
用这种方法,就得手动的去拼接POST的参数数据,注意颜色加深部分代码。
上传多媒体文件,一直返回{"errcode":41005,"errmsg":"media data missing"}
摘自:http://hatustudio.com/weixindeve ... a-missing-2476.html
|
|