1

I've written an application in angular for file download, the application is working fine only for small json string, but comes to large string the download fails. Can anyone please tell me some solution for this.

I'm using REST webservice

 var json = _.getJson(); // here json value object be received from a script function 
 var myURL = 'rest/myMethod?jsonValue=' + JSON.stringify(json);
  _.downloadFile(myURL);

The downloadFile method :

downloadFile: function(URL)
{
    $.fileDownload(URL).done(function(e, response)
    {
      console.log('download success');
    }).fail(function(e, response)
    {
      console.log('fail');
    });
}
4
  • fails how? any errors? just hangs? computer explodes? Commented Mar 14, 2014 at 5:03
  • Are you attaching the JSON to the URL? URL's only work up to about 2000 characters (give or take, depends on browser). Commented Mar 14, 2014 at 5:05
  • @Jorg so what will i do to send such big json string to the server Commented Mar 14, 2014 at 5:08
  • Send it as POST data generally. Angular's $http injector has something for that, too Commented Mar 14, 2014 at 5:09

3 Answers 3

1

As per the comments, here's how to POST using Angular. I can only give you an example here. Header might depend on the angular version etc etc.

function TestController($scope, $http) {
    $http({
        url: 'yourwebsite',
        method: "POST",
        data: json, //this is your json data string
        headers: {
           'Content-type': 'application/json'
        }
    }).success(function (data, status, headers, config) {
        //upload succesfull. maybe update scope with a message?
    }).error(function (data, status, headers, config) {
        //upload failed
    });

}
Sign up to request clarification or add additional context in comments.

6 Comments

then how do i download the file using $.fileDownload
You can't download a url. your comments mention "sending to the server" so that's what I showed. I'm not entirely sure what your xhr request is trying to achieve. Are you sending it to a server in order to generate a file?
actually i'm sending the json to the server and the server will generate an excel based on the json data and i will download it through $.fileDownload
You could send a URL back containing the location of the excel file, and use $http again in the success function or maybe send the whole excel back as binary, but that's a separate question alltogether. You might find an answer in here: stackoverflow.com/questions/3599670/…. The accepted answer looks to be demo'ing an excel file.
which server side language?
|
1

I see two potential problems:

  1. The request URL might be too long (see: this discussion)
  2. The stringified JSON contains characters not valid in a URL

If the URL is too long, you'd have to move the jsonValue into the body of your request rather than passing it as a URL parameter.

To address the second problem, you need to URI encode the stringified value:

var myURL = 'rest/myMethod?jsonValue=' + encodeURIComponent( JSON.stringify(json) );

BTW, looking at tha fail parameters should indicate why the request failed in the first place.

1 Comment

This unfortunately doesn't work for bigger JSON files
0

Here is an example for using $http of Angular for sending a post request and download a XML file from the server.

$http({
            url: '$your_URL_here$', // you should replace $your_URL_here$ by your own url
            method: 'POST',
            responseType: 'arraybuffer',
            data: postJson, //***this is the request json data for post***
            headers: {
                'Content-type': 'application/json',
                'Accept': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
            }
        }).success(function(data){
                var blob = new Blob([data], {
                    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                });                
                saveAs(blob, $fileName$);// you should replace $fileName$ by your own fileName
            }
        }).error(function(data){
            //Some error handling method here
        });

Please note that you should import the FileSaver.js to save the file from 'arraybuffer' responseType.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.