JavaScript/jQuery to download file via POST with JSON data

I have a jquery-based single-page webapp. It communicates with a RESTful web service via AJAX calls.

I'm trying to accomplish the following:

  • Submit a POST that contains JSON data to a REST url.
  • If the request specifies a JSON response, then JSON is returned.
  • If the request specifies a PDF/XLS/etc response, then a downloadable binary is returned.
  • I have 1 & 2 working now, and the client jquery app displays the returned data in the web page by creating DOM elements based on the JSON data. I also have #3 working from the web-service point of view, meaning it will create and return a binary file if given the correct JSON parameters. But I'm unsure the best way to deal with #3 in the client javascript code.

    Is it possible to get a downloadable file back from an ajax call like this? How do I get the browser to download and save the file?

    $.ajax({
        type: "POST",
        url: "/services/test",
        contentType: "application/json",
        data: JSON.stringify({category: 42, sort: 3, type: "pdf"}),
        dataType: "json",
        success: function(json, status){
            if (status != "success") {
                log("Error loading data");
                return;
            }
            log("Data loaded!");
        },
        error: function(result, status, err) {
            log("Error loading data");
            return;
        }
    });
    

    The server responds with the following headers:

    Content-Disposition:attachment; filename=export-1282022272283.pdf
    Content-Length:5120
    Content-Type:application/pdf
    Server:Jetty(6.1.11)
    

    Another idea is to generate the PDF and store it on the server and return JSON that includes a URL to the file. Then, issue another call in the ajax success handler to do something like the following:

    success: function(json,status) {
        window.location.href = json.url;
    }
    

    But doing that means I would need to make more than one call to the server, and my server would need to build downloadable files, store them somewhere, then periodically clean up that storage area.

    There must be a simpler way to accomplish this. Ideas?


    EDIT: After reviewing the docs for $.ajax, I see that the response dataType can only be one of xml, html, script, json, jsonp, text , so I'm guessing there is no way to directly download a file using an ajax request, unless I embed the binary file in using Data URI scheme as suggested in the @VinayC answer (which is not something I want to do).

    So I guess my options are:

  • Not use ajax and instead submit a form post and embed my JSON data into the form values. Would probably need to mess with hidden iframes and such.

  • Not use ajax and instead convert my JSON data into a query string to build a standard GET request and set window.location.href to this URL. May need to use event.preventDefault() in my click handler to keep browser from changing from the application URL.

  • Use my other idea above, but enhanced with suggestions from the @naikus answer. Submit AJAX request with some parameter that lets web-service know this is being called via an ajax call. If the web service is called from an ajax call, simply return JSON with a URL to the generated resource. If the resource is called directly, then return the actual binary file.

  • The more I think about it, the more I like the last option. This way I can get information back about the request (time to generate, size of file, error messages, etc.) and I can act on that information before starting the download. The downside is extra file management on the server.

    Any other ways to accomplish this? Any pros/cons to these methods I should be aware of?


    letronje's solution only works for very simple pages. document.body.innerHTML += takes the HTML text of the body, appends the iframe HTML, and sets the innerHTML of the page to that string. This will wipe out any event bindings your page has, amongst other things. Create an element and use appendChild instead.

    $.post('/create_binary_file.php', postData, function(retData) {
      var iframe = document.createElement("iframe");
      iframe.setAttribute("src", retData.url);
      iframe.setAttribute("style", "display: none");
      document.body.appendChild(iframe);
    }); 
    

    Or using jQuery

    $.post('/create_binary_file.php', postData, function(retData) {
      $("body").append("<iframe src='" + retData.url+ "' style='display: none;' ></iframe>");
    }); 
    

    What this actually does: perform a post to /create_binary_file.php with the data in the variable postData; if that post completes successfully, add a new iframe to the body of the page. The assumption is that the response from /create_binary_file.php will include a value 'url', which is the URL that the generated PDF/XLS/etc file can be downloaded from. Adding an iframe to the page that references that URL will result in the browser promoting the user to download the file, assuming that the web server has the appropriate mime type configuration.


    I've been playing around with another option that uses blobs. I've managed to get it to download text documents, and I've downloaded PDF's (However they are corrupted).

    Using the blob API you will be able to do the following:

    $.post(/*...*/,function (result)
    {
        var blob=new Blob([result]);
        var link=document.createElement('a');
        link.href=window.URL.createObjectURL(blob);
        link.download="myFileName.txt";
        link.click();
    
    });
    

    This is IE 10+, Chrome 8+, FF 4+. See https://developer.mozilla.org/en-US/docs/Web/API/URL.createObjectURL

    It will only download the file in Chrome, Firefox and Opera. This uses a download attribute on the anchor tag to force the browser to download it.


    I know this kind of old, but I think I have come up with a more elegant solution. I had the exact same problem. The issue I was having with the solutions suggested were that they all required the file being saved on the server, but I did not want to save the files on the server, because it introduced other problems (security: the file could then be accessed by non-authenticated users, cleanup: how and when do you get rid of the files). And like you, my data was complex, nested JSON objects that would be hard to put into a form.

    What I did was create two server functions. The first validated the data. If there was an error, it would be returned. If it was not an error, I returned all of the parameters serialized/encoded as a base64 string. Then, on the client, I have a form that has only one hidden input and posts to a second server function. I set the hidden input to the base64 string and submit the format. The second server function decodes/deserializes the parameters and generates the file. The form could submit to a new window or an iframe on the page and the file will open up.

    There's a little bit more work involved, and perhaps a little bit more processing, but overall, I felt much better with this solution.

    Code is in C#/MVC

        public JsonResult Validate(int reportId, string format, ReportParamModel[] parameters)
        {
            // TODO: do validation
    
            if (valid)
            {
                GenerateParams generateParams = new GenerateParams(reportId, format, parameters);
    
                string data = new EntityBase64Converter<GenerateParams>().ToBase64(generateParams);
    
                return Json(new { State = "Success", Data = data });
            }
    
            return Json(new { State = "Error", Data = "Error message" });
        }
    
        public ActionResult Generate(string data)
        {
            GenerateParams generateParams = new EntityBase64Converter<GenerateParams>().ToEntity(data);
    
            // TODO: Generate file
    
            return File(bytes, mimeType);
        }
    

    on the client

        function generate(reportId, format, parameters)
        {
            var data = {
                reportId: reportId,
                format: format,
                params: params
            };
    
            $.ajax(
            {
                url: "/Validate",
                type: 'POST',
                data: JSON.stringify(data),
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                success: generateComplete
            });
        }
    
        function generateComplete(result)
        {
            if (result.State == "Success")
            {
                // this could/should already be set in the HTML
                formGenerate.action = "/Generate";
                formGenerate.target = iframeFile;
    
                hidData = result.Data;
                formGenerate.submit();
            }
            else
                // TODO: display error messages
        }
    
    链接地址: http://www.djcxy.com/p/8048.html

    上一篇: 如何使$ .post()使用contentType = application / json?

    下一篇: JavaScript / jQuery通过POST和JSON数据下载文件