㈠ 微信公眾號JS-SDK的圖片上傳介面為什麼經常上傳失敗的問題我也遇到了,不知您解決沒有
沒有。我自己測試了問題。
感覺除了網路速度慢的問題會造成這類情況。
手機內安裝的安全軟體也會出現攔截的狀況。這類基本都是安卓手機發生。安全軟體或許為了保護流量等原因,把圖片都攔住了。你關閉騰訊管家或者360手機助手,把微信重啟一遍,通常就可以上傳了。
這是我發現的上傳失敗原因的其中一種情況哦。
㈡ 調用微信介面上傳文件 文件以什麼形式上傳
通俗一點,就是接收微信伺服器給你post的xml數據包,你解析處理之後再以xml格式post回去。當然也有json格式的,比如發送客服消息和自定義菜單,二維碼等用的是json格式數據。
㈢ 求一段微信開發新增永久圖片素材的介面代碼
//素材
const MEDIA_FOREVER_UPLOAD_URL = '/material/add_material?';
const MEDIA_FOREVER_NEWS_UPLOAD_URL = '/material/add_news?';
const MEDIA_FOREVER_NEWS_UPDATE_URL = '/material/update_news?';
const MEDIA_FOREVER_GET_URL = '/material/get_material?';
const MEDIA_FOREVER_DEL_URL = '/material/del_material?';
const MEDIA_FOREVER_COUNT_URL = '/material/get_materialcount?';
const MEDIA_FOREVER_BATCHGET_URL = '/material/batchget_material?';
/**
* 上傳臨時素材,有效期為3天(認證後的訂閱號可用)
* 注意:上傳大文件時可能需要先調用 set_time_limit(0) 避免超時
* 注意:數組的鍵值任意,但文件名前必須加@,使用單引號以避免本地路徑斜杠被轉義
* 注意:臨時素材的media_id是可復用的!
* @param array $data {"media":'@Pathfilename.jpg'}
* @param type 類型:圖片:image 語音:voice 視頻:video 縮略圖:thumb
* @return boolean|array
*/
public function uploadMedia($data, $type){
if (!$this->access_token && !$this->checkAuth()) return false;
//原先的上傳多媒體文件介面使用 self::UPLOAD_MEDIA_URL 前綴
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}
/**
* 獲取臨時素材(認證後的訂閱號可用)
* @param string $media_id 媒體文件id
* @param boolean $is_video 是否為視頻文件,默認為否
* @return raw data
*/
public function getMedia($media_id,$is_video=false){
if (!$this->access_token && !$this->checkAuth()) return false;
//原先的上傳多媒體文件介面使用 self::UPLOAD_MEDIA_URL 前綴
//如果要獲取的素材是視頻文件時,不能使用https協議,必須更換成http協議
$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
$result = $this->http_get($url_prefix.self::MEDIA_GET_URL.'access_token='.$this->access_token.'&media_id='.$media_id);
if ($result)
{
if (is_string($result)) {
$json = json_decode($result,true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
}
return $result;
}
return false;
}
/**
* 上傳永久素材(認證後的訂閱號可用)
* 新增的永久素材也可以在公眾平台官網素材管理模塊中看到
* 注意:上傳大文件時可能需要先調用 set_time_limit(0) 避免超時
* 注意:數組的鍵值任意,但文件名前必須加@,使用單引號以避免本地路徑斜杠被轉義
* @param array $data {"media":'@Pathfilename.jpg'}
* @param type 類型:圖片:image 語音:voice 視頻:video 縮略圖:thumb
* @param boolean $is_video 是否為視頻文件,默認為否
* @param array $video_info 視頻信息數組,非視頻素材不需要提供 array('title'=>'視頻標題','introction'=>'描述')
* @return boolean|array
*/
public function uploadForeverMedia($data, $type,$is_video=false,$video_info=array()){
if (!$this->access_token && !$this->checkAuth()) return false;
//#TODO 暫不確定此介面是否需要讓視頻文件走http協議
//如果要獲取的素材是視頻文件時,不能使用https協議,必須更換成http協議
//$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
//當上傳視頻文件時,附加視頻文件信息
if ($is_video) $data['description'] = self::json_encode($video_info);
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_UPLOAD_URL.'access_token='.$this->access_token.'&type='.$type,$data,true);
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}
/**
* 上傳永久圖文素材(認證後的訂閱號可用)
* 新增的永久素材也可以在公眾平台官網素材管理模塊中看到
* @param array $data 消息結構{"articles":[{...}]}
* @return boolean|array
*/
public function uploadForeverArticles($data){
if (!$this->access_token && !$this->checkAuth()) return false;
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPLOAD_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}
/**
* 修改永久圖文素材(認證後的訂閱號可用)
* 永久素材也可以在公眾平台官網素材管理模塊中看到
* @param string $media_id 圖文素材id
* @param array $data 消息結構{"articles":[{...}]}
* @param int $index 更新的文章在圖文素材的位置,第一篇為0,僅多圖文使用
* @return boolean|array
*/
public function updateForeverArticles($media_id,$data,$index=0){
if (!$this->access_token && !$this->checkAuth()) return false;
if (!isset($data['media_id'])) $data['media_id'] = $media_id;
if (!isset($data['index'])) $data['index'] = $index;
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPDATE_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}
/**
* 獲取永久素材(認證後的訂閱號可用)
* 返回圖文消息數組或二進制數據,失敗返回false
* @param string $media_id 媒體文件id
* @param boolean $is_video 是否為視頻文件,默認為否
* @return boolean|array|raw data
*/
public function getForeverMedia($media_id,$is_video=false){
if (!$this->access_token && !$this->checkAuth()) return false;
$data = array('media_id' => $media_id);
//#TODO 暫不確定此介面是否需要讓視頻文件走http協議
//如果要獲取的素材是視頻文件時,不能使用https協議,必須更換成http協議
//$url_prefix = $is_video?str_replace('https','http',self::API_URL_PREFIX):self::API_URL_PREFIX;
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_GET_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
if (is_string($result)) {
$json = json_decode($result,true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return $result;
}
return false;
}
/**
* 刪除永久素材(認證後的訂閱號可用)
* @param string $media_id 媒體文件id
* @return boolean
*/
public function delForeverMedia($media_id){
if (!$this->access_token && !$this->checkAuth()) return false;
$data = array('media_id' => $media_id);
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_DEL_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return true;
}
return false;
}
/**
* 獲取永久素材列表(認證後的訂閱號可用)
* @param string $type 素材的類型,圖片(image)、視頻(video)、語音 (voice)、圖文(news)
* @param int $offset 全部素材的偏移位置,0表示從第一個素材
* @param int $count 返回素材的數量,取值在1到20之間
* @return boolean|array
* 返回數組格式:
* array(
* 'total_count'=>0, //該類型的素材的總數
* 'item_count'=>0, //本次調用獲取的素材的數量
* 'item'=>array() //素材列表數組,內容定義請參考官方文檔
* )
*/
public function getForeverList($type,$offset,$count){
if (!$this->access_token && !$this->checkAuth()) return false;
$data = array(
'type' => $type,
'offset' => $offset,
'count' => $count,
);
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_FOREVER_BATCHGET_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}
/**
* 獲取永久素材總數(認證後的訂閱號可用)
* @return boolean|array
* 返回數組格式:
* array(
* 'voice_count'=>0, //語音總數量
* 'video_count'=>0, //視頻總數量
* 'image_count'=>0, //圖片總數量
* 'news_count'=>0 //圖文總數量
* )
*/
public function getForeverCount(){
if (!$this->access_token && !$this->checkAuth()) return false;
$result = $this->http_get(self::API_URL_PREFIX.self::MEDIA_FOREVER_COUNT_URL.'access_token='.$this->access_token);
if ($result)
{
$json = json_decode($result,true);
if (isset($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}
/**
* 上傳圖文消息素材,用於群發(認證後的訂閱號可用)
* @param array $data 消息結構{"articles":[{...}]}
* @return boolean|array
*/
public function uploadArticles($data){
if (!$this->access_token && !$this->checkAuth()) return false;
$result = $this->http_post(self::API_URL_PREFIX.self::MEDIA_UPLOADNEWS_URL.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}
/**
* 上傳視頻素材(認證後的訂閱號可用)
* @param array $data 消息結構
* {
* "media_id"=>"", //通過上傳媒體介面得到的MediaId
* "title"=>"TITLE", //視頻標題
* "description"=>"Description" //視頻描述
* }
* @return boolean|array
* {
* "type":"video",
* "media_id":"mediaid",
* "created_at":1398848981
* }
*/
public function uploadMpVideo($data){
if (!$this->access_token && !$this->checkAuth()) return false;
$result = $this->http_post(self::UPLOAD_MEDIA_URL.self::MEDIA_VIDEO_UPLOAD.'access_token='.$this->access_token,self::json_encode($data));
if ($result)
{
$json = json_decode($result,true);
if (!$json || !empty($json['errcode'])) {
$this->errCode = $json['errcode'];
$this->errMsg = $json['errmsg'];
return false;
}
return $json;
}
return false;
}
㈣ 微信開發平台中有個介面是上傳多媒體文件,我用的是java 開發的,我怎麼樣才能在後台實現呢代碼如下:
/**
*文件上傳到微信伺服器
*@paramfileType文件類型
*@paramfilePath文件路徑
*@returnJSONObject
*@throwsException
*/
publicstaticJSONObjectsend(StringfileType,StringfilePath)throwsException{
Stringresult=null;
Filefile=newFile(filePath);
if(!file.exists()||!file.isFile()){
thrownewIOException("文件不存在");
}
/**
*第一部分
*/
URLurlObj=newURL("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token="+getAccess_token()+"&type="+fileType+"");
HttpURLConnectioncon=(HttpURLConnection)urlObj.openConnection();
con.setRequestMethod("POST");//以Post方式提交表單,默認get方式
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);//post方式不能使用緩存
//設置請求頭信息
con.setRequestProperty("Connection","Keep-Alive");
con.setRequestProperty("Charset","UTF-8");
//設置邊界
StringBOUNDARY="----------"+System.currentTimeMillis();
con.setRequestProperty("Content-Type","multipart/form-data;boundary="+BOUNDARY);
//請求正文信息
//第一部分:
StringBuildersb=newStringBuilder();
sb.append("--");//必須多兩道線
sb.append(BOUNDARY);
sb.append(" ");
sb.append("Content-Disposition:form-data;name="file";filename=""+file.getName()+"" ");
sb.append("Content-Type:application/octet-stream ");
byte[]head=sb.toString().getBytes("utf-8");
//獲得輸出流
OutputStreamout=newDataOutputStream(con.getOutputStream());
//輸出表頭
out.write(head);
//文件正文部分
//把文件已流文件的方式推入到url中
DataInputStreamin=newDataInputStream(newFileInputStream(file));
intbytes=0;
byte[]bufferOut=newbyte[1024];
while((bytes=in.read(bufferOut))!=-1){
out.write(bufferOut,0,bytes);
}
in.close();
//結尾部分
byte[]foot=(" --"+BOUNDARY+"-- ").getBytes("utf-8");//定義最後數據分隔線
out.write(foot);
out.flush();
out.close();
StringBufferbuffer=newStringBuffer();
BufferedReaderreader=null;
try{
//定義BufferedReader輸入流來讀取URL的響應
reader=newBufferedReader(newInputStreamReader(con.getInputStream()));
Stringline=null;
while((line=reader.readLine())!=null){
//System.out.println(line);
buffer.append(line);
}
if(result==null){
result=buffer.toString();
}
}catch(IOExceptione){
System.out.println("發送POST請求出現異常!"+e);
e.printStackTrace();
thrownewIOException("數據讀取異常");
}finally{
if(reader!=null){
reader.close();
}
}
JSONObjectjsonObj=newJSONObject(result);
returnjsonObj;
}
㈤ 微信介面上傳文件的介面,用postman測試沒有問題,但用js就報403錯誤
你好,解決了嗎?我也是類似於這樣的問題,Postman測試沒問題,網頁就不行,我前端用的是angularJs,後端是springboot,也解決了跨域的問題,但不知道為啥不行
㈥ 企業微信目前有提供自有應用操作文件盤(上傳或下載文件)的介面嗎
我也想問這個問題了 ,樓主有找到答案嗎?
㈦ 前端如何在微信小程序中將用戶頭像存為臨時文件路徑
你可以用wx.request把頭像的url發送到後台伺服器,由後台伺服器去獲取這個圖片。
比如說你的後台程序是php的,可以這樣(假定文件名為save_tx.php):
<?php
$fn=base_convert(time().mt_rand(0,9),10,36)."jpg";
file_put_contents($fn,file_get_contents($_GET['url']));
echo "頭像上傳為".$fn;
?>
這樣在微信小程序中就可以這樣(假定用戶頭像的url已保存到變數txUrl中):
wx.request({
url: '伺服器域名路徑/save_tx.php?url=' + encodeURIComponent(txUrl),
success: res => {
wx.showToast({
title: res,
icon: "none"
})
}
})
㈧ 如何上傳微信後台介面文件
[圖文]2013年4月26日- 如何開啟微信公眾平台的「開發模式」 要開啟開發模式很簡單,只要在後台...那裡要填寫URL和Token信息,URL就是放在你的網站上的介面文件地址, Token...