File URL “Not allowed to load local resource” in the Internet Browser

后端 未结 6 680
北荒
北荒 2020-11-27 19:33

I\'ve got a major brainteaser.

I want to open a file in classic ASP. I\'m using various variables because things can change but the outcome is correct. I know this b

相关标签:
6条回答
  • 2020-11-27 20:00

    You just need to replace all image network paths to byte strings in HTML string. For this first 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-27 20:03

    Follow the below steps,

    1. npm install -g http-server, install the http-server in angular project.
    2. Go to file location which needs to be accessed and open cmd prompt, use cmd http-server ./
    3. Access any of the paths with port number in browser(ex: 120.0.0.1:8080) 4.now in your angular application use the path "http://120.0.0.1:8080/filename" Worked fine for me
    0 讨论(0)
  • 2020-11-27 20:05

    You will have to provide a link to your file that is accessible through the browser, that is for instance:

    <a href="http://my.domain.com/Projecten/Protocollen/346/Uitvoeringsoverzicht.xls">
    

    versus

    <a href="C:/Projecten/Protocollen/346/Uitvoeringsoverzicht.xls">
    

    If you expose your "Projecten" folder directly to the public, then you may only have to provide the link as such:

    <a href="/Projecten/Protocollen/346/Uitvoeringsoverzicht.xls">
    

    But beware, that your files can then be indexed by search engines, can be accessed by anybody having this link, etc.

    0 讨论(0)
  • 2020-11-27 20:17

    For people do not like to modify chrome's security options, we can simply start a python http server from directory which contains your local file:

    python -m SimpleHTTPServer
    

    and for python 3:

    python3 -m http.server
    

    Now you can reach any local file directly from your js code or externally with http://127.0.0.1:8000/some_file.txt

    0 讨论(0)
  • 2020-11-27 20:18

    Now we know what the actual error is can formulate an answer.

    Not allowed to load local resource

    is a Security exception built into Chrome and other modern browsers. The wording may be different but in some way shape or form they all have security exceptions in place to deal with this scenario.

    In the past you could override certain settings or apply certain flags such as

    --disable-web-security --allow-file-access-from-files --allow-file-access
    

    in Chrome (See https://stackoverflow.com/a/22027002/692942)

    It's there for a reason

    At this point though it's worth pointing out that these security exceptions exist for good reason and trying to circumvent them isn't the best idea.

    There is another way

    As you have access to Classic ASP already you could always build a intermediary page that serves the network based files. You do this using a combination of the ADODB.Stream object and the Response.BinaryWrite() method. Doing this ensures your network file locations are never exposed to the client and due to the flexibility of the script it can be used to load resources from multiple locations and multiple file types.

    Here is a basic example (getfile.asp);

    <%
    Option Explicit
    
    Dim s, id, bin, file, filename, mime
    
    id = Request.QueryString("id")
    
    'id can be anything just use it as a key to identify the 
    'file to return. It could be a simple Case statement like this
    'or even pulled from a database.
    Select Case id
    Case "TESTFILE1"
      'The file, mime and filename can be built-up anyway they don't 
      'have to be hard coded.
      file = "\\server\share\Projecten\Protocollen\346\Uitvoeringsoverzicht.xls"     
      mime = "application/vnd.ms-excel"
      'Filename you want to display when downloading the resource.
      filename = "Uitvoeringsoverzicht.xls"
    
    'Assuming other files 
    Case ...
    End Select
    
    If Len(file & "") > 0 Then
      Set s = Server.CreateObject("ADODB.Stream")
      s.Type = adTypeBinary 'adTypeBinary = 1 See "Useful Links"
      Call s.Open()
      Call s.LoadFromFile(file)
      bin = s.Read()
    
      'Clean-up the stream and free memory
      Call s.Close()
      Set s = Nothing
    
      'Set content type header based on mime variable
      Response.ContentType = mime
      'Control how the content is returned using the 
      'Content-Disposition HTTP Header. Using "attachment" forces the resource
      'to prompt the client to download while "inline" allows the resource to
      'download and display in the client (useful for returning images
      'as the "src" of a <img> tag).
      Call Response.AddHeader("Content-Disposition", "attachment;filename=" & filename)
      Call Response.BinaryWrite(bin)
    Else
      'Return a 404 if there's no file.
      Response.Status = "404 Not Found"
    End If
    %>
    

    This example is pseudo coded and as such is untested.

    This script can then be used in <a> like this to return the resource;

    <a href="/getfile.asp?id=TESTFILE1">Click Here</a>
    

    The could take this approach further and consider (especially for larger files) reading the file in chunks using Response.IsConnected to check whether the client is still there and s.EOS property to check for the end of the stream while the chunks are being read. You could also add to the querystring parameters to set whether you want the file to return in-line or prompt to be downloaded.


    Useful Links

    • Using METADATA to Import DLL Constants - If you are having trouble getting adTypeBinary to be recongnised, always better then just hard coding 1.

    • Content-Disposition:What are the differences between “inline” and “attachment”? - Useful information about how Content-Disposition behaves on the client.

    0 讨论(0)
  • 2020-11-27 20:24

    I didn't realise from your original question that you were opening a file on the local machine, I thought you were sending a file from the web server to the client.

    Based on your screenshot, try formatting your link like so:

    <a href="file:///C:/Projecten/Protocollen/346/Uitvoeringsoverzicht.xls">Klik hier</a>
    

    (without knowing the contents of each of your recordset variables I can't give you the exact ASP code)

    0 讨论(0)
提交回复
热议问题