簡體   English   中英

帶有令牌承載javascript的onload函數

[英]onload function with token bearer javascript

我有我的功能

function setImg(imgUrl){
    img.src = imgUrl
    img.onload = function () {
    ctx.canvas.width  = this.width;
    ctx.canvas.height = this.height;
    ctx.drawImage(img, 0,0);
    updateData(lastData)
  };

需要上傳圖像大小並放入畫布中。 上傳圖像需要不記名令牌。

我已經嘗試添加函數的開頭

img.src = imgUrl + '?access_token=xxxxxxxxxxxx';

但它沒有用。 我必須用純 js 來做,我看到了所有其他問題,但沒有一個有幫助。 有人可以幫忙嗎?


我也試着在 setImg 里面做

    let tmp1;

var oReq = new XMLHttpRequest();
oReq.open("GET", imgUrl, true);
oReq.setRequestHeader("Authorization", "Bearer xxxxxx");
// use multiple setRequestHeader calls to set multiple values
oReq.responseType = "arraybuffer";
oReq.onload = function (oEvent) {
  var arrayBuffer = oReq.response; // Note: not oReq.responseText
  if (arrayBuffer) {
    var u8 = new Uint8Array(arrayBuffer);
    var b64encoded = btoa(String.fromCharCode.apply(null, u8));
    var mimetype="image/png"; // or whatever your image mime type is
    // document.getElementById("yourimageidhere").src="data:"+mimetype+";base64,"+b64encoded;
    tmp1 = "data:"+mimetype+";base64,"+b64encoded;
  }
};
oReq.send(null);

img.src = tmp1;
img.onload = function () {
  ctx.canvas.width  = this.width;
  ctx.canvas.height = this.height;
  ctx.drawImage(img, 0,0);
  updateData(lastData)
};

但它沒有用。

const src = imgUrl;
const options = {
  headers: {
    'Authorization': 'Bearer xxxxxxx'
  }
};

fetch(src, options)
.then(res => res.blob())
.then(blob => {
  // console.log("AAA", URL.createObjectURL(blob) );
  img.src = URL.createObjectURL(blob);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM