locked
PDF File being Blocked in Chrome RRS feed

  • Question

  • User438705957 posted

    I have done extensive research on the web for this problem but can't seem to find a fix.

    I have an anchor tag with a File URI schema like so

    <a href="file://pretend_server/pretend_Share_path/Annual Fire Statement/2020_Annual Fire Statement.pdf" target="_blank" class="doco dontPrint"><span class="glyphicon glyphicon-file"></span><span>2017_Annual Fire Statement.pdf</span></a>

    It successfully opens the pdf file in a new tab in IE 11.

    In Chrome, however, clicking the link will not open the file. No message is posted, it just does nothing.
    If I right click on the link to open in another window or tab, I am getting a blocked message in the new tab - about:blank#blocked

    I have disabled the Chrome setting to download pdf files automatically, so they open in the browser by default.

    I have disabled the Chrome Extensions.

    Thanks

    Monday, July 13, 2020 11:52 PM

Answers

  • User1535942433 posted

    Hi Madog,

    As far as I think,you could use javascript and create a new page to pass the filepath and writefile.It should work for all browsers.

    More details,you could refer to below codes:

      <script>
    
            function opendoc(path) {
    
                var pdfFileName = '';
                var pdfPath = '';
                var windowUrl = 'SLA.aspx?FN=' + path;
                var windowId = 'PdfWindow_' + new Date().getTime();
                var windowFeatures =
                    'channelmode=no,directories=no,fullscreen=no,' +
                    'location=no,dependent=yes,menubar=no,resizable=no,scrollbars=yes,' +
                    'status=no,toolbar=no,titlebar=no,';
                var windowReference = window.open(windowUrl, windowId, windowFeatures);
                windowReference.focus();
            }
    
        </script>          
    
    
      <a href="file://C:\Users\yijings\Desktop\1.pdf" target="_blank" class="doco dontPrint" onclick="return opendoc(this.getAttribute('href'))"><span class="glyphicon glyphicon-file"></span><span>2017_Annual Fire Statement.pdf</span></a>
    

    Create a new page,SLA page:

     protected void Page_Load(object sender, EventArgs e)
            {
                string fpath = this.Request["FN"];
                string filename = Path.GetFileName(fpath);
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Disposition", "inline;filename=" + filename);
                Response.ContentType = "application/pdf";
                Response.WriteFile(filename);
                Response.Flush();
                Response.Clear();
    
            }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 14, 2020 3:24 AM

All replies

  • User1535942433 posted

    Hi Madog,

    As far as I think,you could use javascript and create a new page to pass the filepath and writefile.It should work for all browsers.

    More details,you could refer to below codes:

      <script>
    
            function opendoc(path) {
    
                var pdfFileName = '';
                var pdfPath = '';
                var windowUrl = 'SLA.aspx?FN=' + path;
                var windowId = 'PdfWindow_' + new Date().getTime();
                var windowFeatures =
                    'channelmode=no,directories=no,fullscreen=no,' +
                    'location=no,dependent=yes,menubar=no,resizable=no,scrollbars=yes,' +
                    'status=no,toolbar=no,titlebar=no,';
                var windowReference = window.open(windowUrl, windowId, windowFeatures);
                windowReference.focus();
            }
    
        </script>          
    
    
      <a href="file://C:\Users\yijings\Desktop\1.pdf" target="_blank" class="doco dontPrint" onclick="return opendoc(this.getAttribute('href'))"><span class="glyphicon glyphicon-file"></span><span>2017_Annual Fire Statement.pdf</span></a>
    

    Create a new page,SLA page:

     protected void Page_Load(object sender, EventArgs e)
            {
                string fpath = this.Request["FN"];
                string filename = Path.GetFileName(fpath);
                Response.ClearContent();
                Response.ClearHeaders();
                Response.AddHeader("Content-Disposition", "inline;filename=" + filename);
                Response.ContentType = "application/pdf";
                Response.WriteFile(filename);
                Response.Flush();
                Response.Clear();
    
            }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 14, 2020 3:24 AM
  • User-1151440187 posted

    Try this :

    function openTab()
    {
    window.open('2020_Annual Fire Statement.pdf','_blank');
    }

    Hope it'll be help.

    Thanks!

    Tuesday, July 14, 2020 3:55 AM
  • User-474980206 posted

    For security modern browsers only support the file protocol if the html page was loaded with the file protocol. Just like you did with IE, you can update the setting in chrome to allow it.

    but a better approach is to supply a web server link to return the file.

    Tuesday, July 14, 2020 2:38 PM
  • User438705957 posted

    Apologies for not responding sooner. 

    Bruce,
    I cannot find any setting in Chrome that allows that, and I don't remember setting it in IE 11.
    I just found out it doesn't work in Edge either, nor can I find a setting there that works.

    When you say 

    but a better approach is to supply a web server link to return the file.

    do you mean as in Yij Sun's solution.

    Thanks

    Thursday, July 16, 2020 3:38 AM
  • User-474980206 posted

    Sorta. It assumes the same approach but adds extra code.

    you need to write a .net page/action that returns the file. You can use an ID or the file name as a parameter.  If the files are not in the web server, then for it to access the file, the app pool needs to be a domain account with access.

    Thursday, July 16, 2020 2:46 PM
  • User753101303 posted

    Hi,

    For IE it likely depends on the security zone in which is your site (maybe "trusted zone"?) and it could have been configured once as a company wide policy.

    As pointed already it is likely best to stay away from that and have the web server accessing this network share (or a web server side copy) and serving this content over http(s) to the  browser.

    Edit; the rule is that a page served using the http(s) protocol is not supposed to be able to use a resource using the file protocol.

    Thursday, July 16, 2020 5:14 PM