简体   繁体   中英

Open file in intranet from another server - ASP.Net C#

I have a problem. My WebApp have to open file from local resource on another server in browser and if it's not supported with browser then open it with associated program. For exapmle some PDF, DOCX, XLSX files. Path to this server is stored in DB like this: \\server2\\folder\\file.pdf

I have to say that I tried everything I was albe to find and I also found there's no solution. But I really need to figured it out.

My App is ASP.Net AJAX, C# and Telerik Components

What I tried:

- <a href="\\server2\folder\file.pdf">link</a>
- <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("Link") %>' Target="_blank">HyperLink</asp:HyperLink>
- Responce.Redirect(path)

It doesn't work and the responce from Chrome is this:

Not allowed to load local resource: file://server2/folder/file.pdf

Is there any way?

edit - modified specification of the problem

If the client will be accessing this link from outside your internal network, you will need to open the file on the server side and stream it to the client. This would require linking to a new page in your server app with the path you want, and then using that new page to stream the file to the client. For example, your link would look like this:

<a href="/DownloadFile.aspx?path=\\server2\folder\file.pdf">Link</a>

Then in the DownloadFile.aspx codebehind you would have something like this:

var path = Request.QueryString["path"];
Response.WriteFile(path);

这样指定资源URL

 <a href="file://///server2/folder/file.pdf">link</a>

For those who have similar problem as me there is my solution.

There is no way to achieve this. So I had to create new page with parameter with path to file. Then create a memory stream and send this to client.

protected void Page_Load(object sender, EventArgs e)
{
    if (Request.QueryString.AllKeys.Contains("path"))
    {
        string path = Server.UrlDecode(Request.QueryString["path"]);
        if (!(new FileInfo(path).Exists))
            return;

        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                SetStream(fs);
            }
    }
}

private void SetStream(Stream stream)
{
    byte[] bytes = new byte[(int)stream.Length];
    stream.Read(bytes, 0, (int)stream.Length);
    Response.Buffer = true;
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment; filename=" + Server.UrlDecode(Request.QueryString["name"]));
    Response.BinaryWrite(bytes);
    Response.Flush();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM