uni-app小程序处理后端返回的文件流并预览

fileClick(fileName) {
    var fileManager = uni.getFileSystemManager();
    // 文件存储到本地的路径
    var FilePath = wx.env.USER_DATA_PATH + "/" + new Date().getTime() + ".jpg";
    fileManager.writeFile({
        data: data, // 从后台获取的二进制流
        filePath: FilePath,
        encoding: "binary", //编码方式 
        success: result => {
            // 获取图片后缀,自己写的方法
            let fileT = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length)if (fileT == 'png' || fileT == 'jpg') {
                uni.previewImage({
                    urls: [FilePath],
                    current: FilePath,
                    success: () => {},
                    fail: () => { console.log('图片预览失败') },
                });
             } else {
                uni.openDocument({ //我这里成功之后直接打开
                    filePath: FilePath,
                    showMenu: true,
                    fileType: fileT,
                    success: result => { console.log("打开文档成功"); },
                    fail: err => { console.log("打开文档失败", err); }
                });
            }
        },
        fail: err => {
            console.log('失败', err);
        }
    })
}

你可能感兴趣的:(后端,前端,vue.js)