Href wouldn't open file from the server

SA 1 Reputation point
2022-09-02T15:19:38.307+00:00

I have an Excel Marco file on the server and the folder is shared but the following code still wouldn't open the file; nothing happens when clicked on the url. Why is it not opening up the file?
If I type in the url (file:////MyServer/...) in the browser then the file is downloaded automatically and I can open it from the download folder.

<li><a href="file:////MyServer/MyFolder/MySubFolder/MyFile.xlsm" target="_blank">Click to open file</a></li>

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,235 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Michael Taylor 47,461 Reputation points
    2022-09-02T15:43:42.073+00:00

    This is a browser issue. It is a security issue so different browsers behave differently. Most will simply not allow a link to a local file (because you normally don't do that in a website). However if the site is localhost some browsers may allow it. It is also probably dependent upon the zone the site is in.

    One hack that sometimes works is to convert it to http and then use the file path. In other browsers you have to mangle the file path. Refer to this older post on some suggestions. Also refer to the official docs and notice that file:// is not a supported schema by default so it is up to the browser to determine any support at all.

    The preferred workaround is to have the URL point to your app and have your app connect to the share and send the file directly.

    0 comments No comments

  2. SA 1 Reputation point
    2022-09-02T17:14:08.06+00:00

    Thanks but the link you referred did not help.


  3. AgaveJoe 26,031 Reputation points
    2022-09-02T18:50:46.167+00:00

    Simply, you're not following standard web development practices. The href should be a URL. The following example looks in the documents folder located in the web application's root.

    <li><a href="/documents/MyFile.xlsm">Click to open file</a></li>  
    

    If the file is outside the web application then you need to write code that returns a file. The code depends in the type of web application you are building which you have not provided.

    There is no guarantee that Excel will open. It depends in the browser. Usually the file is downloaded to the client machine.

    0 comments No comments

  4. Lan Huang-MSFT 25,156 Reputation points Microsoft Vendor
    2022-09-05T06:18:16.813+00:00

    Hi @SA ,

    file: URLs are system-dependent and only works when the linked page is in the user's computer. But browsers usually use the HTTP protocol, which is http:URL. When you click such a link, nothing happens. The purpose should be security: to prevent remote pages from accessing files on the visitor's computer.

    The way to make the link work is to use the local file as the main file, you can try to get the document from the server using client script, try using the Server.MapPath Method.
    https://learn.microsoft.com/en-us/dotnet/api/system.web.httpserverutility.mappath?view=netframework-4.8
    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments