locked
How to show a local .pdf file as a partial view RRS feed

  • Question

  • User283528319 posted

    Hi all,

    I am trying to show a local pdf file in the browser but I got the error "I can not reach a local file because of security reasons"

    is there any way to breach this "probably the most unnecessary" restriction?

    thanks

    Tuesday, January 8, 2019 6:27 PM

Answers

All replies

  • User753101303 posted

    Hi,

    You would need to use tools to alter settings (edit: for example with IE adding the site to the "trusted zone" should allow local file access) on all the machines (and for all the browsers) on which you want that to work (and it will  never work for machines which are not under your control or you would have to ask users to manually change their browser settings). How do you currently ensure this file is present on the targeted machines ? This not the same file everywhere ?

    Make 100% sure you just can't have this file on your web site. I would suggest to avoid that unless you have a VERY STRONG reason - it would destroy the universe if you can't do that ? ;-)

    Tuesday, January 8, 2019 7:01 PM
  • User283528319 posted

    Hi,

    You would need to use tools to alter settings (edit: for example with IE adding the site to the "trusted zone" should allow local file access) on all the machines (and for all the browsers) on which you want that to work (and it will  never work for machines which are not under your control or you would have to ask users to manually change their browser settings). How do you currently ensure this file is present on the targeted <g class="gr_ gr_20 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="20" data-gr-id="20">machines ?</g> This <g class="gr_ gr_16 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="16" data-gr-id="16">not</g> the same file <g class="gr_ gr_21 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="21" data-gr-id="21">everywhere ?</g>

    Make 100% sure you just can't have this file on your web site. I would suggest <g class="gr_ gr_13 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="13" data-gr-id="13">to avoid</g> that unless you have a VERY STRONG reason - it would destroy the universe if you can't do <g class="gr_ gr_14 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="14" data-gr-id="14">that ?</g> ;-)

    how can I store and back up over 200 <g class="gr_ gr_94 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="94" data-gr-id="94">gb</g> pdf data in my web site? Logical or feasible? 

    Tuesday, January 8, 2019 7:27 PM
  • User475983607 posted

    I am trying to show a local pdf file in the browser but I got the error "I can not reach a local file because of security reasons"

    is there any way to breach this "probably the most unnecessary" restriction?

    As explained in your previous threads, web or browser based applications are very different from desktop applications.  I believe we covered state and db.

    Aside for the concepts in the previous thread, browsers are a sandbox on the client machine.  The browser has very limited access to local resource with good reason.  The last thing you want is a web server having the ability to find and open local files.  

    Tuesday, January 8, 2019 7:29 PM
  • User283528319 posted

    The browser has very limited access to <g class="gr_ gr_14 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="14" data-gr-id="14">local</g> resource with good reason.  The last thing you want is a web server having the ability to find and open local files.  

    but I need to do it. I have to. I don't see any other way to solve this problem.

    Tuesday, January 8, 2019 7:34 PM
  • User475983607 posted

    how can I store and back up over 200 gb pdf data in my web site? Logical or feasible?

    Like a desktop app the files must exist where the application can access the files.  Since web application are hosted on a web server, the files must be located where the web app or web server can access the files.

    Take care when thinking about the web server and client.  A development machine is commonly a web server and client.  This gets developers into trouble when the web app is deployed to a real web host.

    Tuesday, January 8, 2019 7:35 PM
  • User475983607 posted

    fatihbarut

    but I need to do it. I have to. I don't see any other way to solve this problem.

    Move the files to the host. 

    Or explain the problem you are trying to solve not how you think it should be solved.

    Tuesday, January 8, 2019 7:36 PM
  • User753101303 posted

    And currently those files are where ??? If you mean local files they would need to be on each and every machine on which you'll want to access those files which doesn't seems to make sense.

    Forget about any technical solution and explain what you are trying to do ? My guess is that your plan is maybe to access files directly from a browser using a network share available on each and every machine ?

    If yes the web server could use this network share to expose those files through a web UI and then you'll be able to access those files from where you want and won't need access any more to this network share.

    Or if it doesn't help explain what you are tying to do (rather than starting by explaining how you are trying to do something that is still unclear to us).

    Tuesday, January 8, 2019 10:53 PM
  • User283528319 posted

    PatriceSc

    And currently those files are where ??? If you mean local files they would need to be on each and every machine on which you'll want to access those files which doesn't seems to make sense.

    Forget about any technical solution and explain what you are trying to do ? My guess is that your plan is maybe to access files directly from a browser using a network share available on each and every machine ?

    If yes the web server could use this network share to expose those files through a web UI and then you'll be able to access those files from where you want and won't need access any more to this network share.

    Or if it doesn't help explain what you are tying to do (rather than starting by explaining how you are trying to do something that is still unclear to us).

    Yes you are right

    the files are on \\localnetworkserver\mypdfdocuments

    and it would be nice to learn how can I let client browser to reach this server like a web server

    Not: of course I can not let the "web server to expose this pdf files to all over <g class="gr_ gr_118 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins replaceWithoutSep" id="118" data-gr-id="118">internet</g>" I need to restrict unauthorized access to these files too, that is also another issue

    Wednesday, January 9, 2019 6:22 AM
  • User1520731567 posted

    Hi fatihbarut,

    how can I store and back up over 200 gb pdf data in my web site? Logical or feasible? 

    Logic is feasible.

    Add pdf files to the location where the application can access the file.

    How to show a local .pdf file as a partial view

    I make a demo,you could refer to it:

    MyPdfResult_Index View:

    @{
        ViewBag.Title = "MyPdfResult_Index";
    }
    
    <h2>MyPdfResult_Index</h2>
    
    <div>
        @Html.Action("MyPdfResult", "Home")
    </div>

    _Viewpdf partial View:

    @Html.Raw(TempData["Embed"])

    Controller:

    public ActionResult MyPdfResult_Index()
            {
    
                return View();
            }
    
            public ActionResult MyPdfResult()
            {
                string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">";
                embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
                embed += "</object>";
                TempData["Embed"] = string.Format(embed, VirtualPathUtility.ToAbsolute("~/Files/pdf.pdf"));
    
                return PartialView("_Viewpdf", TempData["Embed"]);
            }

    How it works:

    Best Regards.

    Yuki Tao

    Wednesday, January 9, 2019 9:04 AM
  • User283528319 posted

    <g class="gr_ gr_19 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="19" data-gr-id="19">Hi</g> <g class="gr_ gr_17 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="17" data-gr-id="17">fatihbarut</g>,

    fatihbarut

    how can I store and back up over 200 gb pdf data in my web site? Logical or feasible? 

    Logic is feasible.

    Add pdf files to the location where the application can access the file.

    fatihbarut

    How to show a local .pdf file as a partial view

    I make a demo<g class="gr_ gr_21 gr-alert gr_gramm gr_inline_cards gr_run_anim Style replaceWithoutSep" id="21" data-gr-id="21">,you</g> could refer to it:

    MyPdfResult_Index View:

    @{
        ViewBag.Title = "MyPdfResult_Index";
    }
    
    <h2>MyPdfResult_Index</h2>
    
    <div>
        @Html.Action("MyPdfResult", "Home")
    </div>

    _Viewpdf partial View:

    @Html.Raw(TempData["Embed"])

    Controller:

    public ActionResult MyPdfResult_Index()
            {
    
                return View();
            }
    
            public ActionResult MyPdfResult()
            {
                string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">";
                embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
                embed += "</object>";
                TempData["Embed"] = string.Format(embed, VirtualPathUtility.ToAbsolute("~/Files/pdf.pdf"));
    
                return PartialView("_Viewpdf", TempData["Embed"]);
            }

    How it works:

    Best Regards.

    Yuki Tao

    thanks but the thing is I can simply show the pdf file (which is in the server) using the code below 

       <div class="PDF">
    
       <object data="/Content/109538.pdf" type="application/pdf" style="height: 650px; width:700px">alt : <p>Document not found</p>
       </object>
       </div>

    However, I can not show the .pdf  file which is located in a local network server. ( I meant instead of /Content/109538.pdf I need to open the file from \\mynetworkserver\myPDFFilesFolder

    Wednesday, January 9, 2019 9:52 AM
  • User475983607 posted

    However, I can not show the .pdf  file which is located in a local network server. ( I meant instead of /Content/109538.pdf I need to open the file from \\mynetworkserver\myPDFFilesFolder

    Browsers do not allow opening PDF directly from disk.  The browser must stream the file from the web application.  Desktop applications work the same way.  The exception being a Desktop application is installed on a client machine and runs under the context of the logged in user.  A web application is anonymous by design or runs under the context of the application pool.  Therefore, it is up to the developer to manage user state.  

    Anyway, securing files has been solved in web application a long time ago.  Use a handler or a action that returns the file to client.  Use standard security to protect the handler or action.

    Wednesday, January 9, 2019 12:59 PM
  • User753101303 posted

    Ok so the standard approach is to expose this kind of files through the web site either through code (that read files from the network share to send them back to the browser) or directly (in IIS you can create  a "virtual folder" which points to a network share).

    Securing the site is done the same way than for any other site. BTW how do you know which links should be created ? They are registered in a db or you already access those folders to see which files are found and generate those links ? It is all standard stuff done by most if not all "Content Management Systems".

    Though theorically possible the "local path" solutiion would require much more work (ie being able to administer security settings on all machines,and browsers that could be used to access this site and may not fit some cases. And more likely those in charge of the security in your company likely won't accept to lower security for that.

    Wednesday, January 9, 2019 1:24 PM
  • User283528319 posted

    Anyway, securing files has been solved in web application a long time ago.  Use a handler or <g class="gr_ gr_17 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="17" data-gr-id="17">a action</g> that returns the file to <g class="gr_ gr_18 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar only-ins doubleReplace replaceWithoutSep" id="18" data-gr-id="18">client</g>.  Use standard security to protect the handler or action.

    Could you explain how to do it?

    Thursday, January 10, 2019 6:21 AM
  • User753101303 posted

    See for example http://www.prideparrot.com/blog/archive/2012/8/uploading_and_returning_files#filepathresult

    You could start with an hardcoded name to check it works. You'll need to configure IIS so that the account under which your app runs is allowed to access this network share (for example  creating a dedicated account used by youir application pool, see with those in charge of that at your company).

    For the security aspect, you'll have to chose an authentication method :
    - https://www.c-sharpcorner.com/UploadFile/4b0136/introducing-various-authentication-options-in-visual-studio/

    In MVC you can globally require users to be authenticated :
    - https://weblogs.asp.net/jongalloway/asp-net-mvc-authentication-global-authentication-and-allow-anonymous

    This is really common stuff in most if not all applications.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 10, 2019 8:24 AM
  • User283528319 posted

    See for example http://www.prideparrot.com/blog/archive/2012/8/uploading_and_returning_files#filepathresult

    You could start with <g class="gr_ gr_19 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="19" data-gr-id="19">an hardcoded</g> name to check it works. You'll need to configure IIS so that the account under which your app runs is allowed to access this network share (for <g class="gr_ gr_21 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="21" data-gr-id="21">example  creating</g> a dedicated account used by <g class="gr_ gr_20 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="20" data-gr-id="20">youir</g> application pool, see with those in charge of that at your company).

    For the security aspect, you'll have to <g class="gr_ gr_17 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="17" data-gr-id="17">chose</g> an authentication method :
    - https://www.c-sharpcorner.com/UploadFile/4b0136/introducing-various-authentication-options-in-visual-studio/

    In MVC you can globally require users to be authenticated :
    - https://weblogs.asp.net/jongalloway/asp-net-mvc-authentication-global-authentication-and-allow-anonymous

    This is really common stuff in most if not all applications.

    Thanks, this explains the mentality

    Friday, January 11, 2019 11:12 AM