问题描述
我使用C#,我希望用户能够点击一个链接(或一个LinkButton或按钮,我不挑食)有一个网页,并在另存为对话框出现,使他们能够下载文件。本身位于另一台服务器上的文件,所以我必须使用绝对路径(I:\\司\\部门\\ publicfiles \\ filename.pot)。有谁知道该怎么做?
I have a web page using C# where I want users to be able to click a link (or a linkbutton or a button, I'm not fussy) and have the "Save As" dialog window appear so they can download the file. The file itself is located on another server so I have to use the absolute path (i:\division\department\publicfiles\filename.pot). Does anyone know how to do that?
我在这里抬头的问题,有些人建议webClient.DownloadFile。除了因为它要求你已经知道用户想要将文件下载到自己的电脑上,我不能使用它。基本上就是我要找的是当你一个链接上点击右键,选择另存为会发生什么,但是当你离开点击一个链接完成的。
I looked up the question here and some people suggested webClient.DownloadFile. Except I can't use that because it requires that you already know where the user wants the file to be downloaded to on their computer. Basically what I'm looking for is what happens when you right click on a link and select "save as", but done when you left click on a link.
感谢您
推荐答案
我本身位于另一台服务器上的文件听起来可能不被公布到网上,因此不会有一个网址。因此,一个简单的定位标记不会在这种情况下工作。我认为你正在寻找。
"The file itself is located on another server" to me sounds like it might not be published to the web and thus doesn't have a URL. Therefore a simple anchor tag will not work in this case. I think you're looking for Response.TransmitFile()
.
当他们点击链接,你需要发送的文件,并明确设置内容类型和内容disposition头。设置内容处置头附件
,会弹出另存为对话框。赞(未经测试):
When they click on the link, you need to send the file and explicitly set the content type and content-disposition header. Setting the content-disposition header to attachment
will pop up the save as dialog. Like (untested):
Response.ContentType = "application/x-pot";
Response.AppendHeader("Content-Disposition","attachment; filename=filename.pot");
Response.TransmitFile(@"i:\division\department\publicfiles\filename.pot");
Response.End();
这篇关于从链接下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!