FileSystemManager.saveFile

2020-08-26 16:17 更新

解释:保存临时文件到本地。此接口会移动临时文件,因此调用成功后, tempFilePath 将不可用。

Web 态说明:受浏览器限制,Web 态不支持文件系统相关功能,调用该方法会执行失败回调函数。

方法参数:Object object

object参数说明:

success 返回参数说明:

示例 

在开发者工具中打开


图片示例

代码示例

<view class="wrap">
    <button type="primary"  bindtap="saveFile">保存临时文件到本地</button>
</view>
Page({
    data: {
        filePath: ''
    },
    onLoad() {
        this.fileSystemManager = swan.getFileSystemManager();
        swan.downloadFile({
            url: 'https://smartprogram.baidu.com/docs/img/logo.png',
            success: res => {
                swan.showToast({
                    title: `文件预下载完成,临时路径为${res.tempFilePath}`,
                    icon: 'none'
                });
                // 此为 “本地临时文件” 路径。
                console.log(res.tempFilePath);
                this.data.filePath = res.tempFilePath;
            }
        });
    },
    saveFile() {
        this.fileSystemManager.saveFile({
            tempFilePath: this.data.filePath, // 仅为示例,实际上请传真实临时路径地址,如 swan.downloadFile 的 tempFilePath 返回参数
            filePath: `${swan.env.USER_DATA_PATH}/a.png`,
            success: res => {
                swan.showToast({
                    title: res.errMsg,
                    icon: 'none'
                });
                console.log('saveFile success', res);
            },
            fail: err => {
                swan.showToast({
                    title: JSON.stringify(err),
                    icon: 'none'
                });
                console.log('saveFile fail', err);
            }
        });
    }
});


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号