0

私は$.fileDownloadJohn Culviner のプラグインを使用しています。私の目標は、サーバーにあるファイルをダウンロードすることです。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 メソッドにジャンプします。

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 に答える