本文編輯: 技術藍天瀏覽 5424
版權所有,嚴禁轉載
接口 | 說明 |
---|---|
wx.uploadFile(OBJECT) | 將本地資源上傳到開發者服務器。如頁面通過 wx.chooseImage 等接口獲取到一個本地資源的臨時文件路徑后,可通過此接口將本地資源上傳到指定服務器。客戶端發起一個 HTTPS POST 請求,其中 content-type 為 multipart/form-data。 |
wx.downloadFile(OBJECT) | 下載文件資源到本地。客戶端直接發起一個 HTTP GET 請求,返回文件的本地臨時路徑。 |
【代碼】
wxml
<button bindtap="chooseImg">上傳圖片</button>
js
//index.js
var pageObject = {
chooseImg : function(e){
var that = this;
wx.chooseImage({
success: function (res) {
var tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: 'xxxxx', //僅為示例,非真實的接口地址
filePath: tempFilePaths[0],
name: 'file',
formData:{
'user': 'test'
},
success: function(res){
//提示上傳成功
wx.showToast({
title: '上傳成功',
icon: 'success',
duration: 2000
});
}
})
}
})
}
}
Page(pageObject);
wxml
<button type="default" bindtap="chooseImg">上傳圖片</button>
<button type="primary" bindtap="downImg">下載圖片</button>
js
//index.js
//index.js
var pageObject = {
chooseImg : function(e){
var that = this;
wx.chooseImage({
success: function (res) {
var tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: 'xxxxx', //僅為示例,非真實的接口地址
filePath: tempFilePaths[0],
name: 'file',
formData:{
'user': 'test'
},
success: function(res){
//提示上傳成功
wx.showToast({
title: '上傳成功',
icon: 'success',
duration: 2000
});
}
})
}
})
},
downImg : function(e){
wx.downloadFile({
url: 'http://q.qlogo.cn/qqapp/101359334/C28104F08FA21FCABEAD6470BE62543B/100',
success: function(res) {
//提示上傳成功
wx.showToast({
title: '下載成功',
icon: 'success',
duration: 2000
});
console.dir(res);
}
})
}
}
Page(pageObject);
【wx.uploadFile(OBJECT)】:【上傳接口】
將本地資源上傳到開發者服務器。如頁面通過 wx.chooseImage 等接口獲取到一個本地資源的臨時文件路徑后,可通過此接口將本地資源上傳到指定服務器。客戶端發起一個 HTTPS POST 請求,其中 content-type 為 multipart/form-data 。
參數 | 類型 | 描述 |
---|---|---|
url | String | 開發者服務器 url |
filePath | String | 要上傳文件資源的路徑 |
name | String | 文件對應的 key , 開發者在服務器端通過這個 key 可以獲取到文件二進制內容 |
header | Object | HTTP 請求 Header , header 中不能設置 Referer |
formData | Object | HTTP 請求中其他額外的 form data |
success | Function | 接口調用成功的回調函數 |
fail | Function | 接口調用失敗的回調函數 |
complete | Function | 接口調用結束的回調函數(調用成功、失敗都會執行) |
success返回參數:
參數 | 類型 | 描述 |
---|---|---|
data | String | 開發者服務器 url |
statusCode | String | 要上傳文件資源的路徑 |
wx.chooseImage({
success: function(res) {
var tempFilePaths = res.tempFilePaths
wx.uploadFile({
url: 'http://example.weixin.qq.com/upload', //僅為示例,非真實的接口地址
filePath: tempFilePaths[0],
name: 'file',
formData:{
'user': 'test'
},
success: function(res){
var data = res.data
//do something
}
})
}
})
tip: 最大并發限制是 10 個
tip: 默認超時時間和最大超時時間都是 60s
【wx.downloadFile(OBJECT)】:【下載接口】
下載文件資源到本地。客戶端直接發起一個 HTTP GET 請求,返回文件的本地臨時路徑。
參數 | 類型 | 必填 | 描述 |
---|---|---|---|
url | String | 是 | 下載資源的 url |
header | Object | 否 | HTTP 請求 Header |
success | Function | 否 | 下載成功后以 tempFilePath 的形式傳給頁面,res = {tempFilePath: ‘文件的臨時路徑’} |
fail | Function | 否 | 接口調用失敗的回調函數 |
complete | Function | 是 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
success返回參數:
參數 | 類型 | 描述 |
---|---|---|
errMsg | String | 錯誤信息提示 |
statusCode | String | 要上傳文件資源的路徑 |
tempFilePath | String | 文件路徑 |
wx.downloadFile({
url: 'http://example.com/audio/123', //僅為示例,并非真實的資源
success: function(res) {
wx.playVoice({
filePath: res.tempFilePath
})
}
})
tip: 最大并發限制是 10 個
tip: 默認超時時間和最大超時時間都是 60s
tip: 網絡請求的 referer 是不可以設置的,格式固定為 https://servicewechat.com/{appid}/{version}/page-frame.html,其中 {appid} 為小程序的 appid,{version} 為小程序的版本號,版本號為 0 表示為開發版。
tip: 6.5.3 以及之前版本的 iOS 微信客戶端 header 設置無效