从C#通过网络方式通过Ajax下载文件打电话? [英] Download File from C# through Web Method via Ajax call?

查看:109
本文介绍了从C#通过网络方式通过Ajax下载文件打电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我曾尝试通过将WebMethod到从服务器下载文件 但它并没有为我工作。 我的code如下

I have tried to download the file from the server through the webmethod but it has not work for me. my code as below

     [System.Web.Services.WebMethod()]
public static string GetServerDateTime(string msg)
{
    String result = "Result : " + DateTime.Now.ToString() + " - From Server";
    System.IO.FileInfo file = new System.IO.FileInfo(System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["FolderPath"].ToString()) + "\\" + "Default.aspx");
    System.Web.HttpResponse Response = System.Web.HttpContext.Current.Response;
    Response.ClearContent();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
    Response.AddHeader("Content-Length", file.Length.ToString());
    Response.ContentType = "application/octet-stream";
    Response.WriteFile(file.FullName);
    //HttpContext.Current.ApplicationInstance.CompleteRequest();
    Response.Flush();
    Response.End();
    return result;        
}

和我的Ajax调用code是如下

and my ajax call code is as below

    <script type="text/javascript">
    function GetDateTime() {
                    var params = "{'msg':'From Client'}";
                    $.ajax
                      ({
                          type: "POST",
                          url: "Default.aspx/GetServerDateTime",
                          data: params,
                          contentType: "application/json;charset=utf-8",
                          dataType: "json",
                          success: function (result) {
                              alert(result.d);
                          },
                          error: function (err) {

                          }
                      });
    }
</script>

和我呼吁在按钮点击此功能。

and i have called this function in button click..

我不知道如何下载文件与其他方法

i don't know how to download the file with other methods

请给我建议如果有其他方法获得或给予纠正在同一code。

Please suggest me if any other methods available or give the correction in the same code.

感谢所有..

推荐答案

一个WebMethod没有当前的响应流的控制,所以这是不可能做到这一点的方式。那时的你从JavaScript中调用Web方法,响应流已经交付给客户,并且有,你可以做些什么。

A WebMethod does not have control of the current response stream, so this is not possible to do this way. At the time you call a web method from javascript, the response stream is already delivered to the client, and there is nothing you can do about it.

这是选项来做到这一点的是,将WebMethod生成的文件在服务器上的物理的地方文件,然后返回的URL生成的文件的调用JavaScript的,这反过来又使用窗口。打开(...)来打开它。
在生成物理文件代替,你可以调用一些GenerateFile.aspx,做什么,你最初尝试在你的WebMethod,但这样做在的Page_Load ,并调用的window.open('GenerateFile.aspx?味精=从克伦特')从JavaScript。

An option to do this is that the WebMethod generates the file as a physical file somewhere on the server, and then returns the url to the generated file to the calling javascript, which in turn uses window.open(...) to open it.
In stead of generating a physical file, you can call some GenerateFile.aspx that does about what you initially tried in your WebMethod, but do it in Page_Load, and call window.open('GenerateFile.aspx?msg=From Clent') from javascript.

这篇关于从C#通过网络方式通过Ajax下载文件打电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆