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

C# 如何将文件从服务器文件夹保存到本地驱动器/客户端?,c#,asp.net,C#,Asp.net,我在服务器上的某个文件夹中有一个格式化文件。我需要创建一些解决方案,允许用户将此文件保存在计算机的本地驱动器上 谁能告诉我怎么做,我应该使用什么控件。您只需要在aspx页面中添加一个文件链接 <a href="path to your file on server">some text here</a> 当用户单击此链接时,他们将获得下载对话框,使用该对话框他们可以将文件保存到本地系统。您只需在aspx页面中放置文件的链接即可 <a href="path to

我在服务器上的某个文件夹中有一个格式化文件。我需要创建一些解决方案,允许用户将此文件保存在计算机的本地驱动器上


谁能告诉我怎么做,我应该使用什么控件。

您只需要在aspx页面中添加一个文件链接

<a href="path to your file on server">some text here</a>


当用户单击此链接时,他们将获得下载对话框,使用该对话框他们可以将文件保存到本地系统。

您只需在aspx页面中放置文件的链接即可

<a href="path to your file on server">some text here</a>


当用户单击此链接时,他们将获得下载对话框,使用该对话框可以将文件保存到本地系统。

这将打开浏览器另存为对话框:

 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");
}
尝试使用以下权限打开文件流:

  FileStream fs = new FileStream(Server.MapPath("~/imgName.jpg"), FileMode.Open, FileAccess.Read, FileShare.Read);

这将打开“浏览器另存为”对话框:

 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");
}
尝试使用以下权限打开文件流:

  FileStream fs = new FileStream(Server.MapPath("~/imgName.jpg"), FileMode.Open, FileAccess.Read, FileShare.Read);

这不是程序文件保存在客户端上,如果我理解正确,用户将按照帖子中所述启动该过程。该文件是否托管在某个虚拟目录中的IIS服务器上?我希望此文件不包含重要信息。它不是在客户端上保存的编程文件,如果我理解正确,用户将启动本文中提到的过程。此文件是否托管在某个虚拟目录中的IIS服务器上?我希望这个文件不包含重要的信息。这正是我需要的。但是现在在FileStream fs=File.OpenRead(Server.MapPath(“~/imgName.jpg”);它返回异常:文件已被使用!我可以继续吗?如果这不能解决问题,请发布上一段代码,其中使用了file.name.db.mdf文件,即数据库。整个项目都在使用它。我尝试允许一些用户复制数据库文件。这正是我所需要的。但是现在在FileStream fs=File.OpenRead(Server.MapPath(“~/imgName.jpg”);它返回异常:文件已被使用!我可以继续吗?如果这不能解决问题,请发布上一段代码,其中使用了file.name.db.mdf文件,即数据库。整个项目都在使用它。我尝试允许一些用户复制数据库文件。