当我使用blob从ArrayBuffer加载资源时,为什么在Chrome上出现“不允许加载本地资源"错误? [英] Why I got `Not allowed to load local resource` error on chrome when I use blob to load resource from ArrayBuffer?

查看:569
本文介绍了当我使用blob从ArrayBuffer加载资源时,为什么在Chrome上出现“不允许加载本地资源"错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从ArrayBuffer加载图像.我看到一些文章说使用Blob是最有效的方法.

I need to load an image from ArrayBuffer. I saw some articles says using Blob is the most efficient way to make it.

这是我编写的将arraybuffer转换为blob url的代码.

This is the code I wrote to convert arraybuffer to blob url.

        const blob = new Blob([new Uint8Array(arrayBuffer, offset,length)], {
            type: mimeType
        });
        url = window.URL.createObjectURL(blob);

数组缓冲区是数组缓冲区的实例,该实例是通过切片XMLHttpRequest提取的另一个数组缓冲区而创建的.

The array buffer is instance of array buffer that is created by slicing another array buffer fetched by XMLHttpRequest.

然后,我试图像这样从生成的对象URL中获取图像.

And then, I tried to fetch the image from generated object URL like this.

    const imgTag = new Image();
    imgTag.onload = () => {
      resolve(imgTag);
    };
    imgTag.onerror = (e) => {
      reject(e);
    };
    imgTag.src = url;
  });

但是我得到一个错误Not allowed to load local resource.生成的对象URL类似于blob://https://localhost:9443/056abc73-c2d8-47dd-b2c7-24e1966a5221.

But I got an error Not allowed to load local resource. The generated object url is like blob://https://localhost:9443/056abc73-c2d8-47dd-b2c7-24e1966a5221.

我可以在firefox上访问生成的对象URL.而且firefox不会像chrome一样抛出错误.

I could access generated object url on firefox. And firefox won't throw error like chrome.

有什么问题吗?

推荐答案

这是因为chrome出于某些安全原因不允许访问文件系统.

This is because chrome has some security reasons to not allowing access from file system.

打开出现错误的页面并检查URL.

open the page which gets you error and check url.

如果是这样的:

file:///C:/Users/desktopname/projectfolder//yourfile.html

file:///C:/Users/desktopname/projectfolder//yourfile.html

您看到url的第一个单词是 file ,这意味着您正在从文件系统访问文件,而chrome/oprea阻止了您的请求.

you see first word of url is file which means you are accessing file from file system and chrome / oprea are blocking your request.

尝试在 http://localhost/portnumber 上运行项目(此链接可以使您继续运行项目取决于您选择的框架.就我而言,我正在Visual Studio上运行

try to run your project on http://localhost/portnumber (This link you can get on running your project on framework of your choice. in my case, i am running on visual studio)

**我在chrome和Opera网络浏览器上发现了此问题.这适用于mozila firefox和Microsoft Edge.

** I found this issue on chrome and opera web browsers. this working on mozila firefox and microsoft Edge.

这篇关于当我使用blob从ArrayBuffer加载资源时,为什么在Chrome上出现“不允许加载本地资源"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆