uni-app uni.chooseVideo方法 拍摄视频超过一分钟后返回的路径不正确

uni-app uni.chooseVideo方法 拍摄视频超过一分钟后返回的路径不正确

开发环境 版本号 项目创建方式
Windows win10 HBuilderX

产品分类:uniapp/App

PC开发环境操作系统:Windows

PC开发环境操作系统版本号:win10

HBuilderX类型:正式

HBuilderX版本号:3.2.16

手机系统:Android

手机系统版本号:Android 10

手机厂商:vivo

手机机型:vivo Y52s

页面类型:vue

vue版本:vue2

打包方式:云端

项目创建方式:HBuilderX

示例代码:

uni.chooseVideo({ sourceType: sourceType[this.sourceTypeIndex], mediaType: [‘video’], maxDuration: 300, count: 1, success: (res) => { console.log(res) self.uniqueUpload(res.tempFilePath) }, fail: (err) => { console.log(err) } })



uniqueUpload: function(filePath) {
uni.uploadFile({
url: self.config.urlConfig + self.config.PORT + '/fdfs/uploadFile',
name: 'file',
filePath: filePath,
timeout: 300000,
header: {
"Content-Type": "multipart/form-data"
},
success: (res) => {
console.log(res)
},
fail: (res) => {
console.log(res)
}
});
}

操作步骤:

uni.chooseVideo({
sourceType: sourceType[this.sourceTypeIndex],
mediaType: [‘video’],
maxDuration: 300,
count: 1,
success: (res) => {
console.log(res)
self.uniqueUpload(res.tempFilePath)
},
fail: (err) => {
console.log(err)
}
})



uniqueUpload: function(filePath) {  
uni.uploadFile({  
url: self.config.urlConfig + self.config.PORT + '/fdfs/uploadFile',  
name: 'file',  
filePath: filePath,  
timeout: 300000,  
header: {  
"Content-Type": "multipart/form-data"  
},  
success: (res) => {  
console.log(res)  
},  
fail: (res) => {  
console.log(res)  
}  
});  
}

预期结果:

能正确上传


实际结果:


{"errMsg":"uploadFile:fail undefined"}

bug描述:

uni.chooseVideo方法拍摄视频超过一分钟之后返回的路径不对 { “errMsg”: “chooseVideo:ok”, “tempFilePath”: “_doc/uniapp_temp_1638760066688/camera/1638760083759.mp4”, “size”: 169837280, “duration”: 66.9, “width”: 1920, “height”: 1080 } 使用uni.uploadFile上传提示 {“errMsg”:“uploadFile:fail undefined”}


更多关于uni-app uni.chooseVideo方法 拍摄视频超过一分钟后返回的路径不正确的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

请提供完整demo示例

更多关于uni-app uni.chooseVideo方法 拍摄视频超过一分钟后返回的路径不正确的实战教程也可以访问 https://www.itying.com/category-93-b0.html


你让后端看一下,上传的限制大小,如果是springboot的项目,默认有一个上传的限制,如果在上传限制之外,那么就会报错,跟视频地址本身没有关系,如果你,在一分钟之内上传正确,那么99%确定是后端的上传限制问题

根据你的描述,这确实是一个已知的uni-app在Android平台上的文件路径处理问题。当拍摄视频超过一定时长(通常是一分钟左右)时,系统可能会将视频文件存储到不同的临时目录,导致返回的路径格式与uni.uploadFile方法预期的不一致。

问题分析:

  1. 从你提供的返回数据看,tempFilePath"_doc/uniapp_temp_1638760066688/camera/1638760083759.mp4",这个路径格式在Android平台上可能无法被uni.uploadFile正确识别。
  2. 错误信息"uploadFile:fail undefined"通常表示文件路径无效或文件不存在。

解决方案:

方案一:使用文件路径转换(推荐) 在调用uni.uploadFile之前,先将路径转换为正确的格式:

uni.chooseVideo({
    sourceType: sourceType[this.sourceTypeIndex],
    mediaType: ['video'],
    maxDuration: 300,
    count: 1,
    success: (res) => {
        console.log(res)
        
        // 转换文件路径
        let filePath = res.tempFilePath;
        if (filePath.startsWith('_doc/')) {
            filePath = filePath.replace('_doc/', '');
        }
        
        self.uniqueUpload(filePath)
    },
    fail: (err) => {
        console.log(err)
    }
})

方案二:使用plus.io转换路径 对于更复杂的路径处理,可以使用HTML5+ API:

success: (res) => {
    console.log(res)
    
    // 使用plus.io转换路径
    const tempPath = res.tempFilePath;
    plus.io.resolveLocalFileSystemURL(tempPath, function(entry) {
        const nativePath = entry.toLocalURL();
        self.uniqueUpload(nativePath);
    }, function(e) {
        console.log('路径转换失败:', e);
    });
}

方案三:检查文件是否存在 在上传前验证文件:

success: (res) => {
    console.log(res)
    
    // 检查文件是否存在
    plus.io.resolveLocalFileSystemURL(res.tempFilePath, function(entry) {
        entry.file(function(file) {
            if (file.size > 0) {
                self.uniqueUpload(res.tempFilePath);
            } else {
                console.log('文件大小为0,可能路径不正确');
            }
        });
    }, function(e) {
        console.log('文件不存在:', e);
    });
}
回到顶部