微信小程序 網絡API:
wx.uploadFile(OBJECT)
將本地資源上傳到開發者服務器。如頁面通過 wx.chooseImage 等接口獲取到一個本地資源的臨時文件路徑后,可通過此接口將本地資源上傳到指定服務器。客戶端發起一個HTTPS POST請求,其中 Content-Type 為 multipart/form-data 。
OBJECT參數說明:
| 參數 | 類型 | 必填 | 說明 |
|---|---|---|---|
| url | String | 是 | 開發者服務器url |
| filePath | String | 是 | 要上傳文件資源的路徑 |
| name | String | 是 | 文件對應的key , 開發者在服務器端通過這個key可以獲取到文件二進制內容 |
| header | Object | 否 | HTTP 請求 Header |
| formData | Object | 否 | HTTP 請求中其他額外的form data |
| success | Function | 否 | 接口調用成功的回調函數 |
| fail | Function | 否 | 接口調用失敗的回調函數 |
| complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
示例代碼:
wx.chooseImage({ success:function(res){ var tempFilePaths = res.tempFilePaths; wx.uploadFile({ url: 'http://example.com/upload', filePath: tempFilePaths[0], name:"file", formData:{ "user":"test" } }) }})wx.downloadFile(OBJECT)
下載文件資源到本地。客戶端直接發起一個HTTP GET請求,把下載到的資源根據 type 進行處理,并返回文件的本地臨時路徑。
OBJECT參數說明:
| 參數 | 類型 | 必填 | 必填 |
|---|---|---|---|
| url | String | 是 | 下載資源的 url |
| type | String | 否 | 下載資源的類型,用于客戶端識別處理,有效值:image/audio/video |
| header | Object | 否 | HTTP 請求 Header |
| success | Function | 否 | 下載成功后以 tempFilePath 的形式傳給頁面,res={tempFilePath:"文件的臨時路徑"} |
| fail | Function | 否 | 接口調用失敗的回調函數 |
| complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
示例代碼:
wx.downloadFile({ url: 'http://example.com/audio/123', type: 'audio', success:function(res){ wx.playVoice({ filePath: res.tempFilePath }) }})感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答