微信小程序上传图片转为File类型详解
微信小程序中可以使用wx.getFileSystemManager()方法将上传的图片转为file类型,具体步骤如下:
-
在上传图片的回调函数中,获取图片的临时路径
tempFilePath。 -
使用
wx.getFileSystemManager()方法获取文件系统管理器,调用其readFile()方法将临时路径转为file类型。
wx.getFileSystemManager().readFile({
filePath: tempFilePath,
encoding: 'base64',
success: function(res) {
var fileData = res.data;
// 处理file类型数据
}
});
- 在
readFile()方法中,设置encoding为base64,将图片转为base64编码的字符串,然后在success回调函数中处理file类型数据。
注意:使用wx.getFileSystemManager()方法需要用户授权,需要在app.json文件中配置requiredPermission属性。
原文地址: https://www.cveoy.top/t/topic/no8a 著作权归作者所有。请勿转载和采集!