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("完成");
}
});
}
}
})
}
- 发布:2024-09-01 09:19
- 更新:2024-09-02 11:35
- 阅读:148
产品分类: uniCloud/支付宝小程序云
示例代码:
操作步骤:
1、创建vue3项目,并启用uniCloud,选择支付宝云
2、编写 使用uniCloud.uploadFile上传 的 代码
3、运行到浏览器进行
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¶m_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¶m_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¶m_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¶m_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.