Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unable to download the file from ajax call

    In My Web application,

    I want download the file from ajax call.

    reference:Call HTTPhandler from jQuery, Pass data and retrieve in JSON format[^]

    here i am unable to download file, see my code below

    In Handler.ashx file

    Hide Copy Code
    Code:
    public void ProcessRequest(HttpContext context)
            {
    
                string PPTOutputPath = context.Request.QueryString["DownLoadFileName"];
                PPTOutputPath = PPTOutputPath.Replace(",", "\\");
                FileInfo PPTFileNewfile = new FileInfo(PPTOutputPath);
                long sz = PPTFileNewfile.Length;
                context.Response.ClearContent();
                context.Response.ContentType = MimeType(Path.GetExtension(PPTOutputPath));
                context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename = {0}", System.IO.Path.GetFileName(PPTOutputPath)));
                context.Response.AddHeader("Content-Length", sz.ToString("F0"));
                context.Response.TransmitFile(PPTOutputPath);
                context.Response.Flush();
                context.Response.End();
            }
    My ajax call

    Hide Copy Code
    Code:
    function CallHandler(PPTOutputPath) {
                $.ajax({
                    type: 'POST',
                    url: "MyHandler.ashx?DownLoadFileName=" + PPTOutputPath,
                    
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    data: "{'imageData': '" + PPTOutputPath + "'}",
                    responseType: "json",
                    success: OnComplete,
                    error: OnFail
                });
                return false;
            }
    
    function OnComplete(result) {
            alert('Ajax success');
    }
    function OnFail(result) {
        alert('Ajax failed');
    
    }
    Last edited by vinyl-junkie; Oct 4th, 2018 at 09:00 PM. Reason: self-promotional link removed; added code tags

  2. #2
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,125
    Thanks
    4
    Thanked 455 Times in 444 Posts
    You want to what now?!? I don't even think that's a thing. AJAX can load resources from a server, but it can't "download a file" if you mean as in prompting an actual save to the local filesystem.

    Though that ASP code again proves why I'd NEVER use anything ASP related for web technologies. The jQuery isn't helping matters either.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •