【问题标题】:Upload Image to Azure Blob container using Axios [VueJs]使用 Axios [VueJs] 将图像上传到 Azure Blob 容器
【发布时间】:2019-01-21 03:20:19
【问题描述】:

我正在尝试使用 Axios PUT 功能将图像上传到 Azure 存储

我的做法如下:

  1. 我在 Azure 中创建了一个存储帐户,然后我添加了如下 CORS 规则:CORS rule

  2. 我创建了一个名为 user-pic 的 Blob。

  3. 我使用 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!!'); });
}

我所说的代码中的 cmets 是什么意思:

  1. 图片的 URL 应该是这样的格式:Blob SAS Url
  2. Curr_date 的格式是否正确,可以被x-ms-date 标头接受?
  3. 函数btoa是否用于将图像转换为二进制编码?
  4. 是否应该将 Axios 中的所有标头添加到帐户存储 CORS(在标头字段中)?
  5. 获取图像大小的正确方法是什么? (.size 函数?实际上,我在将所有图像附加到其中之后传递了formData

运行程序后,在控制台出现两条错误信息:

我该如何解决这些问题?

更新:

我做了这些改变:

  1. 我更改了 CORS:CORS
  2. 我收到此错误消息:Error Msg

【问题讨论】:

  • Access-Control-Allow-*response 标头,而不是请求标头。
  • @evilSnobu 你是什么意思?

标签: javascript html azure vue.js vuejs2


【解决方案1】:

为 Azure 存储定义的 CORS 规则缺少您的 http://localhost 来源。

【讨论】:

  • 你的意思是我应该将Axios中的所有标题都添加到storge CORS的标题中?
  • 不,我的意思是你必须将你的本地主机源添加到你的 Azure 存储帐户中的允许源(在门户中)
  • 你的意思是我的IP地址,所以它可以访问我的本地主机?
  • 不,我的意思是起源,就像你在这里所做的一样:i.stack.imgur.com/XT8gJ.jpg
  • 请在门户中更改您的 CORS 设置,并为请求和响应标头设置 *。
猜你喜欢
  • 2017-01-06
  • 2021-01-08
  • 2019-05-26
  • 2017-04-26
  • 2015-04-23
  • 1970-01-01
  • 2020-01-09
  • 2021-03-15
  • 2014-07-22
相关资源
最近更新 更多