4***@qq.com
4***@qq.com
  • 发布:2020-08-18 11:41
  • 更新:2020-11-02 14:08
  • 阅读:757

【报Bug】uni.uploadFile oss 直传图片 在hbuilder 更新后 返回图片地址无效 回退 之前版本正常

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

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

HBuilderX类型: 正式

HBuilderX版本号: 2.8.6

手机系统: Android

手机系统版本号: Android 9.0

手机厂商: 华为

手机机型: 荣耀

页面类型: vue

nvue编译模式: fast

打包方式: 云端

项目创建方式: HBuilderX

示例代码:

'''chooseImage(type) {
var that = this;
uni.chooseImage({
count: 1, //默认9
sizeType: ['orginial', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album'], //从相册选择
success: res => {
var path = res.tempFilePaths[0];
this.$request.getSTS().then(res => {
var accessid = res.data.data.accessKeyId;
var accesskey = res.data.data.accessKeySecret;
var securityToken = res.data.data.securityToken;
var expiration = res.data.data.expiration;
console.log('上传');
var uploadFileSize = 1024 1024 100; // 上传文件的大小限制100m
var policyText = {
expiration: expiration, //设置该Policy的失效时间,超过这个失效时间之后,就没有办法通过这个policy上传文件了
conditions: [
['content-length-range', 0, uploadFileSize] // 设置上传文件的大小限制
]
};
var osshost = 'http://rongwodev.oss-cn-shanghai.aliyuncs.com';
var policyBase64 = Base64.encode(JSON.stringify(policyText));

                        var message = policyBase64;  
                        var bytes = Crypto.HMAC(Crypto.SHA1, message, accesskey, {  
                            asBytes: true  
                        });  
                        var signature = Crypto.util.bytesToBase64(bytes);  
                        var timetamp = new Date().getTime();  
                        that.imageSrc = path;  
                        let pos = that.imageSrc.lastIndexOf('.');  
                        console.log(this.imageSrc);  
                        let filename = that.imageSrc.substring(0, pos); // 文件名  
                        let extendName = that.imageSrc.substring(pos + 1); // 扩展名  
                        // 文件路径  
                        let stroeAs = 'rongwodev/' + timetamp + this.random_string(2) + '.' + extendName;  
                        console.log('通');  
                        console.log(osshost);  

                        console.log(that.imageSrc);  
                        console.log(accessid);  
                        console.log(securityToken);  
                        uni.uploadFile({  
                            url: osshost,  
                            filePath: that.imageSrc,  
                            fileType: 'image',  
                            name: 'file',  
                            formData: {  
                                key: stroeAs,  
                                policy: policyBase64,  
                                OSSAccessKeyId: accessid,  
                                'x-oss-security-token': securityToken,  
                                success_action_status: '200', //让服务端返回200,不然,默认会返回204  
                                signature: signature  
                            },  
                            success(res) {  
                                console.log(res);  
                                uni.showToast({  
                                    title: '上传成功',  
                                    icon: 'success',  
                                    duration: 1000  
                                });  
                                if (type === "logo") {  
                                    console.log(osshost + '/' + stroeAs);  
                                    that.logo = osshost + '/' + stroeAs;  
                                } else {  
                                    that.businessLicense = osshost + '/' + stroeAs;  
                                }  

                            },  
                            fail: res => {  
                                console.log(res);  
                            }  
                        });  
                    });  
                }  
            });  
        }, '''

操作步骤:

'''chooseImage(type) {
var that = this;
uni.chooseImage({
count: 1, //默认9
sizeType: ['orginial', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
sourceType: ['album'], //从相册选择
success: res => {
var path = res.tempFilePaths[0];
this.$request.getSTS().then(res => {
var accessid = res.data.data.accessKeyId;
var accesskey = res.data.data.accessKeySecret;
var securityToken = res.data.data.securityToken;
var expiration = res.data.data.expiration;
console.log('上传');
var uploadFileSize = 1024 1024 100; // 上传文件的大小限制100m
var policyText = {
expiration: expiration, //设置该Policy的失效时间,超过这个失效时间之后,就没有办法通过这个policy上传文件了
conditions: [
['content-length-range', 0, uploadFileSize] // 设置上传文件的大小限制
]
};
var osshost = 'http://rongwodev.oss-cn-shanghai.aliyuncs.com';
var policyBase64 = Base64.encode(JSON.stringify(policyText));

                        var message = policyBase64;  
                        var bytes = Crypto.HMAC(Crypto.SHA1, message, accesskey, {  
                            asBytes: true  
                        });  
                        var signature = Crypto.util.bytesToBase64(bytes);  
                        var timetamp = new Date().getTime();  
                        that.imageSrc = path;  
                        let pos = that.imageSrc.lastIndexOf('.');  
                        console.log(this.imageSrc);  
                        let filename = that.imageSrc.substring(0, pos); // 文件名  
                        let extendName = that.imageSrc.substring(pos + 1); // 扩展名  
                        // 文件路径  
                        let stroeAs = 'rongwodev/' + timetamp + this.random_string(2) + '.' + extendName;  
                        console.log('通');  
                        console.log(osshost);  

                        console.log(that.imageSrc);  
                        console.log(accessid);  
                        console.log(securityToken);  
                        uni.uploadFile({  
                            url: osshost,  
                            filePath: that.imageSrc,  
                            fileType: 'image',  
                            name: 'file',  
                            formData: {  
                                key: stroeAs,  
                                policy: policyBase64,  
                                OSSAccessKeyId: accessid,  
                                'x-oss-security-token': securityToken,  
                                success_action_status: '200', //让服务端返回200,不然,默认会返回204  
                                signature: signature  
                            },  
                            success(res) {  
                                console.log(res);  
                                uni.showToast({  
                                    title: '上传成功',  
                                    icon: 'success',  
                                    duration: 1000  
                                });  
                                if (type === "logo") {  
                                    console.log(osshost + '/' + stroeAs);  
                                    that.logo = osshost + '/' + stroeAs;  
                                } else {  
                                    that.businessLicense = osshost + '/' + stroeAs;  
                                }  

                            },  
                            fail: res => {  
                                console.log(res);  
                            }  
                        });  
                    });  
                }  
            });  
        }, '''

预期结果:

返回可以使用的图片地址

实际结果:

图片不可使用

bug描述:

uni.uploadFile 采用oss 直传时 statusCode 返回 403图片地址无效 回退到 2.7.14.20200618 直传图片 返回 statusCode 200 且 图片可用

2020-08-18 11:41 负责人:无 分享
已邀请:
DCloud_UNI_GSQ

DCloud_UNI_GSQ

看一下是不是 signature 计算出的有问题
试试云打包切换到JSC引擎 具体参考 https://ask.dcloud.net.cn/article/37465

  • 4***@qq.com (作者)

    这个可以解决,你们官方说的 : 先把 btoa 的代码注释掉 参考https://ask.dcloud.net.cn/question/77248

    2020-08-18 16:37

景风

景风 - 950005

楼主解决了吗,遇到了同样问题

要回复问题请先登录注册