[Update]Using FileReader in Chrome

大兔子大兔子 提交于 2019-12-10 16:51:42

问题


I set up a Apache server, put the page on the server and-problem solved. So I know this problem is caused by security settings in chrome.

Now I wonder, is it possible to let a local webpage access local files?

I'm going to make a page that allows dragging an local image onto it. Here's my code, which doesn't work in chrome

The problem is reader.onload event didn't trigger, onerror did instead.

So what's the reason? And how to fix it? Many thanks.

var oImg=document.getElementById("img1");

oImg.addEventListener('dragover', function(e) {
    e.stopPropagation();
    e.preventDefault();
}, false);

oImg.addEventListener('drop', handleDrop, false);

function handleDrop(e) {
    e.stopPropagation();
    e.preventDefault();

    var thisfile  = e.dataTransfer.files[0],
    reader = new FileReader();

    reader.onload = (function(thisfile){
        return function(e){
            oImg.src = e.target.result;
            }
        }
    })(thisfile);

    reader.readAsDataURL(thisfile);
}

回答1:


I'm uncertain if this option is still available (AFAIK it is) - try opening chrome with the switch --allow-file-access-from-files

An open issue exists that may be relevant to your question. The more people star this issue the more likely it is to get fixed - see comment #1.




回答2:


The issue appears to be the security constraints around file://

Here is another stack overflow question that talks about the error.

Uncaught Error: SECURITY_ERR: DOM Exception 18 when I try to set a cookie



来源:https://stackoverflow.com/questions/6665457/updateusing-filereader-in-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!