Cannot open local file - Chrome: Not allowed to load local resource

后端 未结 14 1450
不知归路
不知归路 2020-11-22 12:01

Test browser: Version of Chrome: 52.0.2743.116

It is a simple javascript that is to open an image file from local like \'C:\\002.jpg\'

function run(         


        
相关标签:
14条回答
  • 2020-11-22 12:45

    If you have php installed - you can use built-in server. Just open target dir with files and run

    php -S localhost:8001
    
    0 讨论(0)
  • 2020-11-22 12:45

    If you could do this, it will represent a big security problem, as you can access your filesystem, and potentially act on the data available there... Luckily it's not possible to do what you're trying to do.

    If you need local resources to be accessed, you can try to start a web server on your machine, and in this case your method will work. Other workarounds are possible, such as acting on Chrome settings, but I always prefer the clean way, installing a local web server, maybe on a different port (no, it's not so difficult!).

    See also:

    • Open local files(file://) using Chrome
    • Opening local files from chrome
    0 讨论(0)
  • 2020-11-22 12:47

    You just need to replace all image network paths to byte strings in stored Encoded HTML string. For this you required HtmlAgilityPack to convert Html string to Html document. https://www.nuget.org/packages/HtmlAgilityPack

    Find Below code to convert each image src network path(or local path) to byte sting. It will definitely display all images with network path(or local path) in IE,chrome and firefox.

    string encodedHtmlString = Emailmodel.DtEmailFields.Rows[0]["Body"].ToString();
    
    // Decode the encoded string.
    StringWriter myWriter = new StringWriter();
    HttpUtility.HtmlDecode(encodedHtmlString, myWriter);
    string DecodedHtmlString = myWriter.ToString();
    
    //find and replace each img src with byte string
    HtmlDocument document = new HtmlDocument();
    document.LoadHtml(DecodedHtmlString);
    document.DocumentNode.Descendants("img")
        .Where(e =>
        {
            string src = e.GetAttributeValue("src", null) ?? "";
            return !string.IsNullOrEmpty(src);//&& src.StartsWith("data:image");
        })
        .ToList()
        .ForEach(x =>
            {
            string currentSrcValue = x.GetAttributeValue("src", null);                                
            string filePath = Path.GetDirectoryName(currentSrcValue) + "\\";
            string filename = Path.GetFileName(currentSrcValue);
            string contenttype = "image/" + Path.GetExtension(filename).Replace(".", "");
            FileStream fs = new FileStream(filePath + filename, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);
            br.Close();
            fs.Close();
            x.SetAttributeValue("src", "data:" + contenttype + ";base64," + Convert.ToBase64String(bytes));                                
        });
    
    string result = document.DocumentNode.OuterHtml;
    //Encode HTML string
    string myEncodedString = HttpUtility.HtmlEncode(result);
    
    Emailmodel.DtEmailFields.Rows[0]["Body"] = myEncodedString;
    
    0 讨论(0)
  • 2020-11-22 12:47

    I've encounterd this problem, and here is my solution for Angular, I wrapped my Angular's asset folder in encodeURIComponent() function. It worked. But still, I'd like to know more about the risk of this solution if there's any:

    ```const URL = ${encodeURIComponent(/assets/office/file_2.pdf)} window.open(URL)

    I used Angular 9, so this is my url when I clicked open local file:
    ```http://localhost:4200/%2Fassets%2Foffice%2Ffile_2.pdf```
    
    0 讨论(0)
  • 2020-11-22 12:48

    There is a workaround using Web Server for Chrome.
    Here are the steps:

    1. Add the Extension to chrome.
    2. Choose the folder (C:\images) and launch the server on your desired port.

    Now easily access your local file:

    function run(){
       // 8887 is the port number you have launched your serve
       var URL = "http://127.0.0.1:8887/002.jpg";
    
       window.open(URL, null);
    
    }
    run();
    

    PS: You might need to select the CORS Header option from advanced setting incase you face any cross origin access error.

    0 讨论(0)
  • 2020-11-22 12:54

    You won't be able to load an image outside of the project directory or from a user level directory, hence the "cannot access local resource warning".

    But if you were to place the file in a root folder of your project like in {rootFolder}\Content\my-image.jpg and referenced it like so:

    <img src="/Content/my-image.jpg" />
    
    0 讨论(0)
提交回复
热议问题