我正在尝试使用Axios PUT 功能将图像上传到 Azure Storage 。
我所做的如下:
我在Azure中创建了一个存储帐户,然后添加了CORS规则,如下所示:CORS rule
我创建了一个名称为user-pic
的Blob。
我使用 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!!'); });
}
我在代码中的注释是什么意思
Curr_date
头是否正确接受x-ms-date
的格式?btoa
是否用于将图像转换为二进制编码?.size
函数?实际上,我在将所有图像附加到其中之后传递了formData
。运行该程序后,在控制台中,我收到两条错误消息:
我该如何解决这些问题?
更新:
我进行了以下更改: