[英]How save file from server folder to local drive/client side?
I have a formed file in some folder on server. 我在服务器上的某个文件夹中有一个已形成文件。 I need to create some solution to allow user save this file local on drive on computer.
我需要创建一些解决方案,以允许用户将此文件本地保存在计算机的驱动器上。
Can anyone advice me how it can be done, what control I should use. 谁能建议我该怎么做,应该使用什么控件。
This will open the Browser SaveAs dialog: 这将打开“浏览器另存为”对话框:
protected void Page_Load(object sender, EventArgs e)
{
FileStream fs = File.OpenRead(Server.MapPath("~/imgName.jpg"));
byte[] buffer = new byte[(int)fs.Length];
fs.Read(buffer, 0, (int)fs.Length);
fs.Close();
SetResponse("imgName");
HttpContext.Current.Response.BinaryWrite(buffer);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
private static void SetResponse(string fileName)
{
string attachment = "attachment; filename=" + fileName + ".jpg";
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "image/jpeg";
HttpContext.Current.Response.AddHeader("Pragma", "public");
}
try opening the FileStream with this permissions: 尝试使用以下权限打开FileStream:
FileStream fs = new FileStream(Server.MapPath("~/imgName.jpg"), FileMode.Open, FileAccess.Read, FileShare.Read);
You will just need to put a link of your file in aspx page. 您只需要在aspx页面中放置文件链接即可。
<a href="path to your file on server">some text here</a>
When user click on this link they will got the download dialog box using which they can save the file to there local system. 当用户单击此链接时,他们将获得下载对话框,通过该对话框可以将文件保存到本地系统。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.