从服务器下载文件的最佳方式是什么

时间:2021-10-08 18:11:09

I have interesting task which requires me to download a dynamically generated file from a server (ASP.NET) to the client. On the client side it is just JavaScript (jQuery) and the client is able to generate a lot of parameters to customize how the data is exported.

我有一个有趣的任务,它要求我从服务器(ASP.NET)下载一个动态生成的文件到客户端。在客户端,它只是JavaScript (jQuery),客户端能够生成许多参数来定制如何导出数据。

What is the best way to do download the file from the server? Should I use a WCF service such as what is described here or simple page like this one?

从服务器下载文件的最佳方式是什么?我应该使用WCF服务(如这里所描述的那样),还是使用像这样的简单页面?

I don't know how to download a file without reloading the page (I'm not sure that $.ajax will work in this case). Could someone please give me some direction on this topic? Thanks.

我不知道如何在不重载页面的情况下下载文件(我不确定是不是$)。ajax将在这种情况下工作。关于这个问题,谁能给我一些建议吗?谢谢。

1 个解决方案

#1


20  

First you can create the file from a handler .ashx

首先,可以从处理程序.ashx创建文件

Let say that you have the file for downloading at download.ashx and you have some parametres to pass from your javascript, eg download.ashx?p1=8827&p2=8831 to know what you going to create.

假设您有下载文件。ashx和你有一些参数可以从你的javascript中传递,例如download.ashx?p1=8827&p2=8831,知道你要创建什么。

Then on your javascript you simple can make a redirect as

然后在您的javascript上,您可以简单地进行重定向

window.location = "download.ashx?p1=8827&p2=8831";

or alternative you can use the window.open for do the same think

或者你也可以使用这个窗口。打开同样的思路

window.open("download.ashx?p1=8827&p2=8831");

and your file will start the download.

你的文件将开始下载。

Just make sure that you have set the header of attachment, and the correct contenttype on your handle eg:

只要确定你已经设置了附件的标题,以及你的句柄上正确的内容类型:

  HttpContext.Current.Response.ContentType = "application/octet-stream";
  HttpContext.Current.Response.AddHeader("Content-Disposition", 
                    "attachment; filename=" + SaveAsThisFileName);

Simple and clear, both tested and working.

简单和清晰,测试和工作。

Also you may interesting on this answer: How to handle errors.

您可能还会对这个答案感兴趣:如何处理错误。

#1


20  

First you can create the file from a handler .ashx

首先,可以从处理程序.ashx创建文件

Let say that you have the file for downloading at download.ashx and you have some parametres to pass from your javascript, eg download.ashx?p1=8827&p2=8831 to know what you going to create.

假设您有下载文件。ashx和你有一些参数可以从你的javascript中传递,例如download.ashx?p1=8827&p2=8831,知道你要创建什么。

Then on your javascript you simple can make a redirect as

然后在您的javascript上,您可以简单地进行重定向

window.location = "download.ashx?p1=8827&p2=8831";

or alternative you can use the window.open for do the same think

或者你也可以使用这个窗口。打开同样的思路

window.open("download.ashx?p1=8827&p2=8831");

and your file will start the download.

你的文件将开始下载。

Just make sure that you have set the header of attachment, and the correct contenttype on your handle eg:

只要确定你已经设置了附件的标题,以及你的句柄上正确的内容类型:

  HttpContext.Current.Response.ContentType = "application/octet-stream";
  HttpContext.Current.Response.AddHeader("Content-Disposition", 
                    "attachment; filename=" + SaveAsThisFileName);

Simple and clear, both tested and working.

简单和清晰,测试和工作。

Also you may interesting on this answer: How to handle errors.

您可能还会对这个答案感兴趣:如何处理错误。