2

I have some xml files stored as strings in my database and scala+spring based backend with this controller:

@RequestMapping(value = Array("/download"), method = Array(RequestMethod.GET))
def downloadFile(@RequestParam filename: String, //some more params
                 response: HttpServletResponse) = {
  val fileContent = // some logic here, returns file content as String
  response.setContentType("application/xml")
  response.setHeader("Content-Disposition", s"attachment;filename=$filename")
  response.setStatus(HttpServletResponse.SC_OK)
  response.getOutputStream.write(fileContent.getBytes)
  response.flushBuffer()
}

Also i have this script:

$.ajax({
     type: "GET",
     url: url,
     data: {
         filename: filename //and some more params
     }
})

Then i send HTTP request to server, get right HTTP response and then nothing happens. All info i have from browser logs is that response has file content in body and headers, but download never starts.

What am i doing wrong?

I saw these SO Q&A but they didnt help me at all:

download file using an ajax request

Downloading a file from spring controllers

UPD1: Also tried this one, with no result.

Community
  • 1
  • 1
Everv0id
  • 1,862
  • 3
  • 25
  • 47

1 Answers1

0

This is how I made a file to download from a server.

    output = (byte[]) processedDocumentObject;
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    responseHeaders.setContentDispositionFormData("attachment", "file.xml");
    HttpEntity<byte[]> fileEntity = new HttpEntity<byte[]>(output,responseHeaders);
    return fileEntity;

However this is in java and HttpHeaders is org.springframework.http.HttpHeaders and HttpEntity is org.springframework.http.HttpEntity<byte[]>

Also, you need to convert your string into byte array initially.

Pallav Jha
  • 3,409
  • 3
  • 29
  • 52