javascript - 使用 Axios [VueJs] 将图像上传到 Azure Blob 容器
问题描述
我正在尝试使用 Axios PUT功能将图像上传到Azure Storage。
我所做的如下:
我在 Azure 中创建了一个存储帐户,然后按如下方式添加了 CORS 规则:CORS 规则
我创建了一个带有 name 的 Blob
user-pic
。我使用Axios为我提出请求
代码:
function upload(formData) {
//get the current date
var currentdate = new Date();
var Curr_date = currentdate.getDay + '-' + currentdate.getMonth + '-' + currentdate.getFullYear;
//Here I am trying to convert the image to binary encoding.
var data = btoa(formData);
//The image Url, [ below there is an example from where I take this url ].
const url = "https://XXXXXXX.blob.core.windows.net/XXXXXXXXXXXX";
//Headers are required for me, do I need to add all Headers in my code also in CORS of the storage account?
axios.put(url, data {
headers: {
"Access-Control-Allow-Origin" : "*",
'Access-Control-Allow-Methods': 'GET, POST, PATCH, PUT, DELETE, OPTIONS',
"Access-Control-Allow-Headers": "Origin, Content-Type, x-ms-*",
"Content-Type": "image/png",
"Content-Length": data.length, //here I am trying to get the size of image.
"x-ms-date": Curr_date,
"x-ms-version": sv,
"x-ms-blob-type": "BlockBlob",
}
})
.then ( response => { console.log(response); console.log('correct!!'); } )
.catch ( error => { console.log(error); console.log('error here!!'); });
}
我的意思是代码中的注释:
- 图片 URL 的格式应与此相同:Blob SAS Url
- 的格式
Curr_date
是否正确可以被x-ms-date
标头接受? - 函数是否
btoa
用于将图像转换为二进制编码? - 我应该将 Axios 中的所有标头添加到帐户存储 CORS(在标头字段中)吗?
- 获取图像大小的正确方法是什么?(
.size
函数?实际上,我formData
在将所有图像附加到它之后传递。
运行程序后,在控制台中,我收到两条错误消息:
我该如何解决这些问题?
更新:
我做了这些改变:
解决方案
为 Azure 存储定义的 CORS 规则缺少http://localhost源。
推荐阅读
- css - 如何在样式组件上保持字体大小从父级到子级?
- java - 为什么我们不能将 RSocket 与 Apache Tomcat 一起使用?
- zsh - brew 升级后 PoweLevel9k 崩溃
- python - 在 python try-except 子句的 unittest 中未捕获 AssertError
- python-3.x - 我在我的代码中两次使用相同的模型,如何避免转移权重?
- java - 在不检查存在的情况下解决损坏的符号链接
- html - Safari中的引导列+垂直选项卡受到干扰
- android - 如何计算Android手机从手上掉下来的垂直距离?
- reactjs - 如何在 React.js 中获取 URL 值?
- katalon-studio - 我们可以为katalon studio中的文本字段生成随机的唯一字母吗?