0

I am using the $.fileDownload Plugin from John Culviner, my goal is to download a file which is located on a server. I am using ASP.NET Web Forms. Here is my code which I use.

My JavaScript code is this

$("#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);    
                                    }
                         }                       
                    }); 
});

My "ServerCode" a WCF Webservice

[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();

}

Can someone say me what I have configure wrong, the $.fileDownload method jump always in the failCallback method.

4

2 回答 2

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");
}
于 2013-06-19T08:21:52.700 回答
0

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

于 2013-06-19T09:33:50.137 回答