W3Cschool
恭喜您成為首批注冊(cè)用戶(hù)
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
saveFile 會(huì)把臨時(shí)文件移動(dòng),因此調(diào)用成功后傳入的 tempFilePath 將不可用。
解釋?zhuān)罕4嫖募奖镜兀镜匚募鎯?chǔ)的大小限制為 10M。
Web 態(tài)說(shuō)明: 受瀏覽器限制,Web 態(tài)不支持文件系統(tǒng)相關(guān)功能,調(diào)用該方法會(huì)執(zhí)行失敗回調(diào)函數(shù)。
Object object
屬性名 | 類(lèi)型 | 必填 | 默認(rèn)值 | 說(shuō)明 |
---|---|---|---|---|
tempFilePath | String | 是 | 需要保存的文件的臨時(shí)路徑 | |
success | Function | 否 | 接口調(diào)用成功的回調(diào)函數(shù) | |
fail | Function | 否 | 接口調(diào)用失敗的回調(diào)函數(shù) | |
complete | Function | 否 | 接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行) |
參數(shù)名 | 類(lèi)型 | 說(shuō)明 |
---|---|---|
savedFilePath | String | 文件的保存路徑 |
<view class="wrap">
<view class="card-area">
<view class="display-area">
<image class="display-area-image" src="https://b.bdstatic.com/searchbox/icms/searchbox/img/file-pdf.png" rel="external nofollow" mode="widthFix"></image>
<view class="title">示例文件.pdf</view>
</view>
</view>
<button type="primary" bindtap="saveFile">保存文件</button>
</view>
Page({
saveFile() {
// 先把服務(wù)器上文件下載下來(lái)生成臨時(shí)文件路徑,再保存到本地,不支持網(wǎng)絡(luò)路徑
swan.downloadFile({
header: {
'Cache-Control': 'no-cache'
},
url: 'https://example.com/file/xxx', // 僅為示例,并非真實(shí)的接口地址
success: res => {
console.log('臨時(shí)文件路徑:', res);
swan.saveFile({
tempFilePath: res.tempFilePath,
success: res => {
console.log('保存到的路徑是:', res.savedFilePath);
swan.showModal({
title: '保存成功',
content: '保存到的路徑是:' + res.savedFilePath
});
},
fail: err => {
swan.showToast({
title: '保存失敗',
icon: 'none'
});
}
});
}
});
}
});
錯(cuò)誤碼 | 說(shuō)明 |
---|---|
201 | 解析失敗,請(qǐng)檢查調(diào)起協(xié)議是否合法 |
1001 | 執(zhí)行失敗 |
2000 | 文件路徑無(wú)效 |
2001 | 文件不存在 |
2002 | 文件大小超過(guò)限制 |
2003 | IO 異常 |
錯(cuò)誤碼 | 說(shuō)明 |
---|---|
202 | 解析失敗,請(qǐng)檢查參數(shù)是否正確 |
2001 | 文件不存在 |
2002 | 文件大小超過(guò)限制 |
2003 | IO 異常 |
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: