uni-app 文件上传API在H5下不正常

uni-app 文件上传API在H5下不正常

文件上传API问题

重现步骤

uni.chooseImage({  
    success: (chooseImageRes) => {  
        const tempFilePaths = chooseImageRes.tempFilePaths;  
        uni.uploadFile({  
            url: 'https://www.example.com/upload', //仅为示例,非真实的接口地址  
            filePath: tempFilePaths[0],  
            name: 'file',  
            success: (uploadFileRes) => {  
                console.log(uploadFileRes.data);  
            }  
        });  
    }  
});

结果 微信小程序或者APP上传正常

运行到H5 h5运行的时候没有文件后缀名称

期望 上传成功

IDE运行环境说明

环境 版本
HBuilderX 1.2
操作系统 Windows 10

App运行环境说明

环境 版本
Android -

附件

联系方式 [QQ]


更多关于uni-app 文件上传API在H5下不正常的实战教程也可以访问 https://www.itying.com/category-93-b0.html

10 回复

h5版的确没有扩展名,和上传API没有关系。现在设计的文件路径是没有扩展名的。

更多关于uni-app 文件上传API在H5下不正常的实战教程也可以访问 https://www.itying.com/category-93-b0.html


这样子的话,后台的程序怎么获取扩展名呢?

是否应该考虑解决一下这个问题

@phpjishu@qq.com:后期优化和完成文件相关API,可能会补上扩展名,但是扩展名是不准确的,真实的文件类型可以从文件头判断。

回复 DCloud_UNI_GSQ: 为什么 APP iOS 后缀是大写的呢

回复 叫啥好呢: 具体说来

回复 DCloud_UNI_GSQ: 在APP iOS中 获取到的图片后缀是大写的JPG PNG

回复 叫啥好呢: 特定文件吗?还是特定机型这样?

1.3版将优化此问题,1.3体验版可以在官方交流群群里下载。

这是一个H5平台下uni.uploadFile的文件名处理问题。在H5环境下,uni-app的上传API会丢失原始文件名信息,导致服务器接收不到文件后缀。

解决方案是在H5平台手动处理文件名:

uni.chooseImage({
    success: (chooseImageRes) => {
        const tempFilePaths = chooseImageRes.tempFilePaths;
        const fileName = tempFilePaths[0].substring(tempFilePaths[0].lastIndexOf('/') + 1);
        
        uni.uploadFile({
            url: 'https://www.example.com/upload',
            filePath: tempFilePaths[0],
            name: 'file',
            formData: {
                'filename': fileName // 额外传递文件名参数
            },
            success: (uploadFileRes) => {
                console.log(uploadFileRes.data);
            }
        });
    }
});
回到顶部