使用jQuery fileDownload插件从服务器下载文件

时间:2013-06-19 08:01:57

标签: jquery asp.net webforms download

我正在使用John Culviner的$.fileDownload插件,我的目标是下载位于服务器上的文件。我正在使用 ASP.NET Web窗体。这是我使用的代码。

我的JavaScript代码是

$("#btnDownload").click(function () {
                     $.support.cors = true;
                     $.ajax({
                         type: "GET",
                         url: "http://localhost/GetData/9", 
                         processdata: false, 
                         success: function (msg) {  
                                    $.fileDownload("http://localhost/fileDownload.txt", {  
                                       successCallback: function (url) {  
                                         alert('You just got a file download dialog or ribbon for this URL :' + url); 
                                    },  
                                       failCallback: function (html, url) {    
                                         alert('Your file download just failed for this URL:' + url + '\r\n' +     
                                              'Here was the resulting error HTML: \r\n' + html);    
                                    }
                         }                       
                    }); 
});

我的“ServerCode”是WCF Web服务

[WebInvoke(UriTemplate = "GetDaten/{value}", Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public void GetData(string value)
{
            HttpContext.Current.Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/" });

            HttpContext.Current.Response.ContentType = "text/plain";
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=fileDownload.txt");
            HttpContext.Current.Response.WriteFile(HttpContext.Current.Server.MapPath("~/fileDownload.txt"));
            HttpContext.Current.ApplicationInstance.CompleteRequest();

}

有人能说我配置错误,$。fileDownload方法总是在failCallback方法中跳转。

2 个答案:

答案 0 :(得分:0)

试试这个:

$("#btnDownload").click(function () {
        $.support.cors = true;                    
        $.fileDownload("http://localhost/fileDownload.txt", {  
               successCallback: function (url) {  
                   alert('You just got a file download dialog or ribbon for this URL :' + url); 
                   },  
                   failCallback: function (html, url) {    
                   alert('Your file download just failed for this URL:' + url + '\r\n' +     
                                          'Here was the resulting error HTML: \r\n' + html);    
                   }
        }                      

});

我认为第一个ajax请求是不必要的。

因为这也使得请求: $ .fileDownload

服务器代码: 在演示(MVC3应用程序)中,他使用控制器将文件从服务器发送到客户端。 在控制器内部有一个FilePathResult类型函数。 尝试这种方式,也许它有效,但我现在无法尝试。

public FilePathResult GetData(string value)
{  
    return File("~/fileDownload.txt", "text/plain", "fileDownload.txt");
}

答案 1 :(得分:0)

这里有一个解决方案:WCF Streaming: Upload/Download Files Over HTTP 也许你可以与你的jquery插件集成。