如图片中那样,后端返回的是文件流,在小程序和app中如何处理文件流

3***@qq.com
- 发布:2023-12-25 18:27
- 更新:2023-12-25 19:00
- 阅读:1182

1***@163.com - 在技术的道路上缓慢前进
在uniapp开发中,可以使用uni.request或uni.downloadFile方法从后端获取文件流。获取到文件流后,可以使用uni.arrayBufferToBase64方法将文件流转换为base64编码的字符串,然后在小程序或app中使用base64编码的字符串进行处理。
以下是一个示例代码:
uni.request({
url: 'http://example.com/file',
responseType: 'arraybuffer',
success: (res) => {
const base64 = uni.arrayBufferToBase64(res.data)
// 在小程序或app中使用base64编码的字符串进行处理
}
})
另外,如果需要在小程序或app中下载文件,可以使用uni.downloadFile方法,该方法会将文件下载到本地临时文件夹中,然后可以使用uni.getFileSystemManager方法将文件读取为文件流进行处理。
以下是一个示例代码:
uni.downloadFile({
url: 'http://example.com/file',
success: (res) => {
uni.getFileSystemManager().readFile({
filePath: res.tempFilePath,
encoding: 'binary',
success: (res) => {
// 在小程序或app中使用文件流进行处理
}
})
}
})
3***@qq.com (作者)
谢谢,我尝试一下
2023-12-26 08:44
3***@qq.com (作者)
请问一下我现在的文件流是pdf,我转换成了base64编码之后如何预览呢
2023-12-26 09:47
1***@163.com
回复 3***@qq.com: 插件市场看看有没有pdf预览插件!
2023-12-26 10:45
z***@njkingdee.com
回复 3***@qq.com: 你好,后续有解决吗
2024-09-27 11:38