晨石
晨石
  • 发布:2024-09-01 09:19
  • 更新:2024-09-02 11:35
  • 阅读:52

【报Bug】支付宝云上传文件到内置存储报错vue3

分类:uniCloud

产品分类: uniCloud/支付宝小程序云

示例代码:
selectFile() {  
                let that = this;  
                uni.chooseFile({  
                    count: 1,  
                    extension: this.fileTypes,  
                    sourceType:['album'],  
                    success: function(res) {  
                        console.log(res);  
                        console.log(JSON.stringify(res.tempFilePaths));  
                        if (res.tempFilePaths.length > 0) {  
                            let filePath = res.tempFilePaths[0];  
                            let fileName = res.tempFiles[0].name;  
                            let fileExtension = that.getExtension(fileName);  
                            if (that.fileTypes.indexOf('.' + fileExtension) == -1) {  
                                uni.showModal({  
                                    title: "上传失败",  
                                    content: "请选择正确的文件格式",  
                                    showCancel: false  
                                })  
                                return  
                            }  
                            //进行上传操作  
                            uniCloud.uploadFile({  
                                filePath: filePath,  
                                cloudPath: 'img/' + fileName,  
                                onUploadProgress: function(progressEvent) {  
                                    console.log(progressEvent);  
                                    var percentCompleted = Math.round(  
                                        (progressEvent.loaded * 100) / progressEvent.total  
                                    );  
                                },  
                                success(res) {  
                                    console.log("上传完成", res);  
                                    that.updateValue(res.fileID)  
                                },  
                                fail(err) {  
                                    console.log("上传出错", err);  
                                },  
                                complete() {  
                                    console.log("完成");  
                                }  
                            });  
                        }  
                    }  
                })  
            }

操作步骤:

1、创建vue3项目,并启用uniCloud,选择支付宝云
2、编写 使用uniCloud.uploadFile上传 的 代码
3、运行到浏览器进行

预期结果:

浏览器控制台错误结果:
Access to XMLHttpRequest at 'https://u.object.cloudrun.cloudbaseapp.cn/miniapp-prod-env-00jxh6arfjpy-hz/?use_dynamic=1&source_product=oss&creator=2021004166655952&acl=default&file_id=cloud://env-00jxh6arfjpy/img/2.bmp&content_type=application/x-bmp&param_sign=9a86324bd8e6ea0e67c53428a61710e3a0a80311' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

实际结果:

浏览器控制台错误结果:
Access to XMLHttpRequest at 'https://u.object.cloudrun.cloudbaseapp.cn/miniapp-prod-env-00jxh6arfjpy-hz/?use_dynamic=1&source_product=oss&creator=2021004166655952&acl=default&file_id=cloud://env-00jxh6arfjpy/img/2.bmp&content_type=application/x-bmp&param_sign=9a86324bd8e6ea0e67c53428a61710e3a0a80311' from origin 'http://localhost:5173' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

bug描述:

vue2正常,vue3报错
错误信息:has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

2024-09-01 09:19 负责人:无 分享
已邀请:
DCloud_uniCloud_CRL

DCloud_uniCloud_CRL

配置一下跨域,把 localhost:5173 添加进去。

要回复问题请先登录注册