简体   繁体   English

图片删除(在dropBox公用文件夹中)时出现CORS错误-Javascript

[英]CORS error on image drop (in dropBox public folder) - Javascript

I am getting an (CORS policy) error while trying to load an image file by dragging and dropping it on the canvas. 尝试通过将图像文件拖放到画布上来加载图像文件时,出现(CORS策略)错误。

Error : Cross-origin image load denied by Cross-Origin Resource Sharing policy. 错误:跨域资源共享策略拒绝跨域图像加载。

Here is my code to load Image 这是我的代码加载图像

function loadDroppedImages(listOfImages){
images_Object = [];
var loadchecker = new Array();

for( var i=0;i<listOfImages.length;i++){
    var img = new Image();
    img.crossOrigin = "";
            // To enable CORS as anonymous user
    var fileReader = new FileReader();
    fileReader.onload = function(event){
        img.src =event.target.result;
    }
    fileReader.readAsDataURL(listOfImages[i]);
}

}

I am running the page through dropbox public folder and I want to enable CORS here. 我正在通过保管箱公用文件夹运行页面,我想在此处启用CORS。

I also have following lines in my html document. 我的html文件中也有以下几行。

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://dropBox/examplePath/">
    <CORSRule>
        <AllowedOrigin>*</AllowedOrigin>
        <AllowedMethod>PUT</AllowedMethod>
        <AllowedMethod>POST</AllowedMethod>
        <AllowedMethod>GET</AllowedMethod>
        <AllowedMethod>HEAD</AllowedMethod>
        <MaxAgeSeconds>3000</MaxAgeSeconds>
        <AllowedHeader>*</AllowedHeader>
    </CORSRule>
</CORSConfiguration>

Thanks 谢谢

Is there a reason you have to read the file with FileReader ? 您是否必须使用FileReader读取文件的原因? I assume that Dropbox public folders don't set the CORS headers to allow this, so the error is somewhat expected. 我假设Dropbox公用文件夹没有设置CORS标头来允许这样做,因此有些错误是可以预期的。

That said, if all you're doing is displaying the image in an <img> tag, why don't you just set the src attribute to the URL directly, instead of reading the file in code? 也就是说,如果您要做的只是在<img>标签中显示图像,为什么不直接将src属性设置为URL,而不是通过代码读取文件?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM