简体   繁体   English

如何将文件从服务器文件夹保存到本地驱动器/客户端?

[英]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.

相关问题 如何将.pdf文件从服务器保存到客户端? - How to save .pdf file from server to client side? 如何将 aspx 文件另存为 html 文件和文件夹到本地计算机或服务器 - how to save aspx file as html file and folder to local machine or server 从客户端保存到sql server数据库 - save to sql server database from client side 如何使用 C# 从谷歌驱动器下载文件并保存到本地文件夹 - How to download files from google drive and save to a folder on local using C# 创建表客户端,然后下载excel文件或在服务器端生成excel文件,保存到服务器,然后使用JQuery从那里下载? - Create table client side, then download excel file OR generate excel file server-side, save to server, and use JQuery to download from there? 如何将下载的文件保存到本地文件夹? - How to save downloaded file to local folder? 从Websphere MQ读取文件(非字符串)并保存到本地驱动器 - Read File (not string) from Websphere MQ and save in to local drive 使用C#应用程序客户端在服务器上使用文件和文件夹 - working with file&folder on server with c# application client side C#如何将所有文件从服务器下载到本地文件夹 - C# how to download all file from server to local folder 如何从 web 客户端/服务器打开本地文件 - How to open a local file from a web client/server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM