强制下载 Web 服务器上的文件 - ASP .NET C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/873207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:00:20  来源:igfitidea点击:

Force download of a file on web server - ASP .NET C#

c#asp.netdownload

提问by Sameet

I need to force the initiation of download of a .sql file, when user clicks a button in my ASP .NET (C#) based web application.

当用户单击基于 ASP .NET (C#) 的 Web 应用程序中的按钮时,我需要强制启动 .sql 文件的下载。

As in, when the button is clicked, a save as dialog should open at the client end...

如在单击按钮时,应在客户端打开一个另存为对话框...

How do I do this?

我该怎么做呢?

EDIT

编辑

This is the code I am using

这是我正在使用的代码

        string sql = "";
        using (System.IO.StreamReader rdr = System.IO.File.OpenText(fileName))
        {
            sql = rdr.ReadToEnd();
        }
        Response.ContentType = "text/plain";
        Response.AddHeader("Content-Disposition", "attachment; filename=Backup.sql");
        Response.Write(sql);
        Response.End();

This is the error that I'm getting...

这是我得到的错误...

alt text http://img40.imageshack.us/img40/2103/erroro.gif

替代文字 http://img40.imageshack.us/img40/2103/erroro.gif

What's wrong?

怎么了?

采纳答案by Mehrdad Afshari

Create a separate HTTP Handler (DownloadSqlFile.ashx):

创建一个单独的 HTTP 处理程序 (DownloadSqlFile.ashx):

<%@ WebHandler Language="C#" Class="DownloadHandler" %>

using System;
using System.Web;

public class DownloadHandler : IHttpHandler {
    public void ProcessRequest(HttpContext context) {
        var fileName = "myfile.sql";
        var r = context.Response;
        r.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
        r.ContentType = "text/plain";
        r.WriteFile(context.Server.MapPath(fileName));
    }
    public bool IsReusable { get { return false; } }
}

Then make the button in your ASP.NET page navigate to DownloadSqlFile.ashx.

然后使 ASP.NET 页面中的按钮导航到DownloadSqlFile.ashx.

回答by Neil Trodden

You need to tell the browser that what you are sending is not html and shouldn't be displayed as such in the browser. The following code can be used to return some text in your server-side code and it will present the save dialog to the user as you wanted:

您需要告诉浏览器您发送的不是 html,不应在浏览器中显示。以下代码可用于在服务器端代码中返回一些文本,并将根据需要向用户显示保存对话框:

Response.Clear(); //eliminates issues where some response has already been sent
Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.sql", filename));
Response.Write(yourSQL);
Response.End();

filename: the name you wish to give it

filename: 你想给它起的名字

yourSQL: the content of the sql file

yourSQL: sql文件的内容

回答by Greg Ogle

Add a Response.Clearbefore you Response.Writeto fix the error, if stated properly in the Chrome dialog.

如果在 Chrome 对话框中正确说明,Response.ClearResponse.Write在修复错误之前添加一个。

So to integrate in the snippet in other answer...

因此,要集成到其他答案中的代码段中...

//add this
Reponse.Clear(); 

//from other answer
Response.ContentType = "text/plain";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.sql", filename));
Response.Write(yourSQL);
Response.End;

回答by Ryan Farley

The problem is that you're using the code on the same page as is currently loaded in the browser. You are attempting to modify the response stream and disposition of the current loaded page.Instead, create a separate HttpHandler, as suggested by Mehrdad. Then on the click of the button, you will invoke the URL to the handler. The button could even be a simple hyperlink that had the following as the source URL:

问题是您在浏览器中当前加载的同一页面上使用代码。您正在尝试修改当前加载页面的响应流和处置。相反,按照 Mehrdad 的建议,创建一个单独的 HttpHandler。然后单击按钮,您将调用处理程序的 URL。该按钮甚至可以是一个简单的超链接,将以下内容作为源 URL:

<a href="DownloadSqlFile.ashx">Download SQL</a>

Make sense? The point being, you cannot modify the response of the already loaded page. Launch a new request using a handler to do the work.

有道理?关键是,您不能修改已加载页面的响应。使用处理程序启动一个新请求来完成这项工作。

回答by Reginardo Tribuzi Lula Júnior

The solution below works for me

下面的解决方案对我有用

string fileNameAndExtension = "thisFile.pdf";     
string fullPath = "../folder/files/" + fileNameAndExtension;       
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileNameAndExtension );
Response.TransmitFile(fullPath);
Response.End();