I have to download the xml file from the http response header data manually. I am getting the file size using xhr but It couldn't download the file

Viewed 18

The Code I am having in the java method is having xml content in a stringbuffer object and writing it to outputstreamwriter as a xml file.

        String xmlContnt = new String(outputXMLCont.toString().getBytes(),"UTF-8");
        
        response.setContentType("application/xml");
        
        response.addHeader("Content-Disposition", "attachment; filename="+fileName);
        
        OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream(), "ISO-8859-1");
        osw.write(xmlContnt);//xmlcontnt is stringbuffer obj with xml tags
        osw.flush();
        osw.close();

The frontend code I am using to call that java method is helping to get the file size but not downloading the file in the response header.

        function getfile(url, callback) {
        var xhr = new XMLHttpRequest();
        xhr.open("HEAD", url, true); // Notice "HEAD" instead of "GET",
                                     //  to get only the header
        xhr.onreadystatechange = function() {
            if (this.readyState == this.DONE) {
                callback(parseInt(xhr.getResponseHeader("Content-Length")));
            }
        };
        xhr.send();
    }

    getfile("servlet/exportXML", function(size) {
        alert("The size of file is: " + size + " bytes.");
    });
0 Answers
Related