uni-app APP上 uni.chooseImage转换出来的tempFiles不是文件

uni-app APP上 uni.chooseImage转换出来的tempFiles不是文件

开发环境 版本号 项目创建方式
Windows 安卓11 HBuilderX
HBuilderX 3.3.11
Android Android 11
手机厂商 手机机型 页面类型
华为 note9 vue
vue2

操作步骤:

  • APP上 uni.chooseImage转换出来的tempFiles不是文件,只是一个对象

预期结果:

  • APP上 uni.chooseImage转换出来的tempFiles是文件

实际结果:

  • APP上 uni.chooseImage转换出来的tempFiles不是文件

bug描述:

  • APP上 uni.chooseImage转换出来的tempFiles不是文件,只是一个对象

更多关于uni-app APP上 uni.chooseImage转换出来的tempFiles不是文件的实战教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

打印结果是什么

更多关于uni-app APP上 uni.chooseImage转换出来的tempFiles不是文件的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在 UniApp 中,使用 uni.chooseImage 方法选择图片后,返回的 tempFilePathstempFiles 是临时文件路径或文件对象。这些临时文件通常存储在应用的临时目录中,并且是文件对象或路径字符串。

如果你发现 tempFiles 不是文件对象,可能是因为以下原因:

  1. 返回的是路径字符串uni.chooseImage 返回的 tempFilePaths 是图片的临时路径数组,tempFiles 是文件对象数组。如果你使用的是 tempFilePaths,它返回的是路径字符串,而不是文件对象。

    uni.chooseImage({
      success: (res) => {
        console.log(res.tempFilePaths); // 临时文件路径数组
        console.log(res.tempFiles); // 文件对象数组
      }
    });
    
  2. 平台差异: 不同的平台(如微信小程序、H5、App)在处理 tempFiles 时可能会有差异。在某些平台上,tempFiles 可能是一个包含文件信息的对象数组,而不是标准的 File 对象。

  3. 需要手动转换为文件对象: 如果你需要将路径字符串转换为文件对象,可以使用 uni.uploadFile 或其他方法来处理这些临时文件。

    uni.chooseImage({
      success: (res) => {
        const tempFilePaths = res.tempFilePaths;
        tempFilePaths.forEach((filePath) => {
          uni.uploadFile({
            url: 'https://example.com/upload',
            filePath: filePath,
            name: 'file',
            success: (uploadRes) => {
              console.log(uploadRes.data);
            }
          });
        });
      }
    });
    
  4. 检查平台支持: 确保你使用的平台支持 tempFiles 属性。在某些平台上,tempFiles 可能不可用或不完全支持。

如果你需要进一步处理这些临时文件,可以根据具体需求选择合适的方法,比如上传、预览、保存等。如果你需要将路径转换为文件对象,可以使用 uni.getFileSystemManager().readFile 或其他相关 API 来读取文件内容并创建文件对象。

uni.chooseImage({
  success: (res) => {
    const tempFilePaths = res.tempFilePaths;
    tempFilePaths.forEach((filePath) => {
      uni.getFileSystemManager().readFile({
        filePath: filePath,
        encoding: 'binary',
        success: (readRes) => {
          const file = new File([readRes.data], 'filename', { type: 'image/jpeg' });
          console.log(file);
        }
      });
    });
  }
});
回到顶部