小程序wx.uploadFile上传视频报错
小程序wx.uploadFile上传视频报错:
在开发工具上传时,报错如下:
在真机调试中,报错如下:
安卓版本号:Android10;
手机:小米8
2 回复
//读取文件
afterRead(e) {
console.log(e.detail);
const file = e.detail.file;
const index = file.url.lastIndexOf('.');
const type = file.url.substring(index + 1, file.url.length);
const flag = type == 'mp4'||type == 'mp3';
if(!flag){
app.showToast('文件格式只支持mp4、mp3', 'none');
return false;
}
this.setData({
file: file,
isNoFile: false
});
this.uploadCloudFile(file.url);
},
//上传文件接口
uploadCloudFile(filePath){
console.log(filePath)
app.api.uploadCloudFile(filePath).then((res)=>{
console.log('上传成功',res);
}).catch((err)=>{
console.log('上传失败',err);
});
},
<van-uploader max-size="{{200*1024*1024}}" bind:oversize="oversize" bind:after-read="afterRead" accept="video">
<van-button type="info" size="small">上传视频文件</van-button>
<view class="upload-tip">格式:mp4</view>
<view class="err-tip" wx:if="{{isNoFile}}">请上传音视频文件</view>
</van-uploader>
//api
uploadCloudFile(filePath) {
return http.upload({
url: '/attacheFile/uploadFile',
filePath: filePath,
name: 'file'
});
},
//http
upload({
url,
filePath,
name,
formData = {}
}) {
return new Promise((resolve, reject) => {
wx.uploadFile({
url: url,
filePath: filePath,
name: name,
formData: formData,
success(res) {
let data = JSON.parse(res.data);
if (data.status != 0) {
reject(data.message)
} else {
resolve(data.data)
}
},
fail: (error) => {
reject(error)
}
})
});
}