简体   繁体   English

创建文件服务器上文件的下载链接

[英]Creating download link to a file on a file server

I'm looking for a way to (easily, by preference ;)) create a download link to a file on a separate file server. 我正在寻找一种方法(轻松,优先;))创建一个单独的文件服务器上的文件的下载链接。

The situation is as follows: the application I'm developing (asp.net 2.0 in vb.net but I have a similar issue in c#, either solution works for me) will be run internally for a company. 情况如下:我正在开发的应用程序(vb.net中的asp.net 2.0,但我在c#中有类似的问题,无论哪种解决方案适用于我)都将在公司内部运行。 As is good practice, the file storage and web application are on two separate servers. 好的做法是,文件存储和Web应用程序位于两个单独的服务器上。

I basically need to be able to create a download link to a file, the only available URL i have to access the file is \\servername\\folder1\\folder2\\folder3\\file.txt (can be any sort of file) 我基本上需要能够创建一个文件的下载链接,我必须访问该文件的唯一可用URL是\\ servername \\ folder1 \\ folder2 \\ folder3 \\ file.txt (可以是任何类型的文件)

Weblinks simply don't work. Weblinks根本不起作用。 This is how it's currently set up: 这是它目前的设置方式:

tablerowfield.Text = String.Format(
    "<a href=""\\servername\folder1\folder2\folder3\{0}"" 
        target=""_blank"">Click me</a>",
    filename)

Which doesn't work for obvious reasons. 由于显而易见的原因,这不起作用。 It used to be set up to write that file to the application path itself and that worked perfectly, but it isn't good practice and that's why I'm changing it (or trying to). 它曾经被设置为将该文件写入应用程序路径本身并且完美地工作,但这不是好的做法,这就是我改变它(或尝试)的原因。

I read solutions about creating a download page and then having a table in your DB which holds the links and returns the proper web URL for download but the time constraint I am faced with unfortunately doesn't allow me to develop that. 我阅读了有关创建下载页面的解决方案,然后在数据库中有一个表格,其中包含链接并返回适当的Web URL以供下载,但遗憾的是我面临的时间限制不允许我开发它。

Assuming I can provide a string with the full filepath to the file like the above, what is the easiest way to just create a link that, when clicked, downloads the document? 假设我可以像上面那样为文件提供一个包含完整文件路径的字符串,那么创建链接的最简单方法是什么?单击时,下载文档?

Note: I have 0 admin rights in this environment. 注意:我在此环境中拥有0个管理员权限。 That really isn't helping me. 这真的没有帮助我。 Let's assume I am given the correct link like above and have the appropriate file access rights and such. 让我们假设我得到了如上所述的正确链接,并拥有相应的文件访问权限等。

UPDATE: 更新:

The above example does work in IE, but not in Firefox and Chrome. 以上示例适用于IE,但不适用于Firefox和Chrome。 IE converts it to a file://servername/... link which does what it's supposed to, but FF and Chrome both actively decided that this is unsafe and have disabled it from their browsers. IE将其转换为文件:// servername / ...链接,它可以实现预期,但FF和Chrome都主动决定这是不安全的,并已从浏览器中禁用它。

You can use ASHX file (say, downloadfile.ashx) and use the following code (not tested, but it will be something like that) in it: 您可以使用ASHX文件(例如,downloadfile.ashx)并使用以下代码(未经过测试,但它会是类似的):

 Response.Clear();
 Response.ContentType = "application/octet-stream";
 Response.AddHeader("Content-Disposition", "attachment; filename=abc.txt");                                            
 Response.WriteFile(Server.MapPath("\\servername\folder1\folder2\folder3\abc.txt"));
 Response.End();

and then use this in your anchor tag like: 然后在你的锚标签中使用它,如:

<a href="downloadfile.ashx"  target=""_blank"">Click me</a>

Note: You can also pass parameters for downloading different files like: 注意:您还可以传递用于下载不同文件的参数,例如:

<a href="downloadfile.ashx?file=abc.txt"  target=""_blank"">Click me</a>

and then, in ashx file, use the file name to download the appropriate file. 然后,在ashx文件中,使用文件名下载相应的文件。

this piece of code will create a file in download folder with name=hi.txt and content as "thanks god, finally file got downloaded." 这段代码将在下载文件夹中创建一个文件,其名称为= hi.txt,内容为“感谢上帝,最后文件已下载”。

 Response.Clear();
 Response.ContentType = "application/octet-stream";
 Response.AddHeader("Content-Disposition", "attachment; filename=hi.txt");
 Response.Write("thanks god, finally file got downloaded.");
 Response.End();

If your file is already there on server then you can use this code in your download button click event like this 如果您的文件已经存在于服务器上,那么您可以在下载按钮单击事件中使用此代码

protected void downloadpdf_Click(object sender, EventArgs e)
{
    Response.Clear();
    Response.ContentType = "application/octet-stream";
    Response.AddHeader("Content-Disposition", "attachment; filename=""downloadName.pdf""");
    Response.WriteFile(Server.MapPath(@"~/path of pdf/actualfile.pdf"));
    Response.End();
}

\\\\servername\\folder1\\folder2\\folder3\\... is an UNC path which cannot be used from a browser. \\\\servername\\folder1\\folder2\\folder3\\...是一个无法从浏览器使用的UNC路径。 Because the file(s) are on a separate server, you need an href attribute of the form http://server-name/folder1/folder2/file.txt . 由于文件位于单独的服务器上,因此需要http://server-name/folder1/folder2/file.txt形式的href属性。

If the server-name is unresolvable by the clients, then you need to first get the IP address of the server and then formulate the href of the form: http://10.1.1.30/folder1/folder2/file.txt 如果客户端无法解析服务器名称,则需要首先获取服务器的IP地址,然后制定表单的href: http://10.1.1.30/folder1/folder2/file.txthttp://10.1.1.30/folder1/folder2/file.txt

Here is how you get the IP address from server-name: 以下是从server-name获取IP地址的方法:

IPAddress[] host;
host = Dns.GetHostAddresses("server-name");
string ip = host[0].ToString();         

EDIT: 编辑:

I basically need to be able to create a download link to a file 我基本上需要能够创建一个文件的下载链接

With the ashx solution your application would be reading the file from the server and relaying it to the clients rather than just providing the clients a link to download the file directly from the file server. 使用ashx解决方案,您的应用程序将从服务器读取文件并将其中继到客户端,而不仅仅是为客户端提供直接从文件服务器下载文件的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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