私は$.fileDownload
John 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 メソッドにジャンプします。