Как скачать файл с httpServlet с помощью Jquery?

В моем приложении объект json создается на стороне клиента. Этот объект публикуется в HttpServlet, который создает файл PDF на основе данных POST.

Файл отправляется обратно пользователю. Вызывается функция succes, и данные потока регистрируются. Однако я хочу, чтобы файл был загружен.

Как этого добиться?

Мой клиентский код:

$(document).ready(function() {
// when the print button is clicked
$('#exportButton').click(function() {

    var tableIdx = performanceDetailTableController.getTableIdx();

    var allData = {
        "shipTable1":{
            "rows":[
                {  "latitude":"12323","longitude":"213213"},
                {  "latitude":"213213","longitude":"543543"}
            ]},
        "shipTable2":{
            "rows":[
                {  "latitude":"12323", "longitude":"213213"},
                {  "latitude":"213213","longitude":"543543"}
            ]}
        }

    var postData = JSON.stringify(allData);

    $.ajax({
        type : "POST",
        url : 'pdfServlet',
        contentType: "application/json; charset=utf-8",
        data : postData,
                    async : false,
        success : function(data) {
            alert("got some data");
            console.log(data);
        },
    });
});

});

И сервлет, создающий файл:

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

    // get the json content
    StringBuffer jsonContent = getPostedContent(request);

    log.info(jsonContent.toString());

    // convert json to pojo's
    Tables tables = getTablesFromString(jsonContent);

    // create a xml stream
    ByteArrayOutputStream xml = new XmlConverter().getXMLSource(tables);

    // put the xml on the request
    request = setXmlOnRequest(request, xml);

    // create pdf data of the pdf-able xml content
    ByteArrayOutputStream pdf = new PdfHandler().createPdfDataStream(request);

    // response = createResponseheaders(response, request);
    response.setContentType("application/pdf");
    response.setContentLength(pdf.size());
    response.setHeader("Content-disposition", "attachment; filename=test.pdf");
    response.setCharacterEncoding("utf-8");
    response.getOutputStream().write(pdf.toByteArray());

    //close the streams
    pdf.close();
    response.getOutputStream().close();
}

Результат в журнале: *

%PDF-1.4
%
4 0 obj
< /Producer (Apache FOP Version SVN branches/fop-0_95)
/CreationDate (D:20130725162007+02'00')
>>
endobj
5 0 obj
<   /N 3
  /Length 11 0 R
  /Filter /FlateDecode
>>
stream
xwTSϽ7PhRHH.*1  J 

Ответы на вопрос(1)

Ваш ответ на вопрос