0

I'm trying to open a document from my machine to the browser. This is my code:

        string script = string.Format("window.open(\"file:{0}\", \"_blank\");", tempSubFolderPath.Replace(@"\", @"/"));

        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, UpdatePanel1.GetType(), "OpenDoc", script, true);

When I debug it, this is what I get for the script:

window.open("file://MyMachineName/Temp/hw4ynub4.zsx", "_blank");

I even tried by providing the path with the document name like this:

      string script = string.Format("window.open(\"file:{0}\", \"_blank\");", TempDocPath.Replace(@"\", @"/"));

and this is when I debug what I get for the script:

window.open("file://MyMachineName/Temp/qmr2qodz.tfb/20180221_182350.doc", "_blank");

It didn't work with both options. The page opens as a blank page. The document exists in my machine and I can access to it but I can't open it in a new window in the browser. Please advise. Thanks!

IT Developer
  • 23
  • 2
  • 10
  • I think your browser won't allow you to. Even when you are on localhost, it prevents access from `http://` to `file://`. Some browser like Chrome allow to bypass this by starting it with an additional parameter, see https://stackoverflow.com/questions/18586921/how-to-launch-html-using-chrome-at-allow-file-access-from-files-mode – Martin Braun Feb 22 '18 at 00:15
  • Some browsers doesn't allow `file://` path for security measures. You can use `FileStream` with `Server.MapPath` to map file location, then put an iframe in HTML and pass the URL of your file there or use third party document rendering library (since DOC file has binary format, not an ordinary text file). – Tetsuya Yamamoto Feb 22 '18 at 01:44

0 Answers0