Dosyayı ajax üzerinden sunucudan indirmem gerekiyor. Sorun, dosyanın sunucuda depolanmamasıdır. Benim java tabanlı arka uç otomatik istek parametrelerden dosyası oluşturur ve tepki vücutta döndürür: Dosya indirme ajax aracılığıyla
@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(@RequestParam String description, @RequestParam Long logId, HttpServletResponse response) {
try {
InputStream fileContent = // getting file as byte stream
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
response.setHeader("Content-Disposition", "attachment; filename=file.zip");
ServletOutputStream responseOutputStream = response.getOutputStream();
org.apache.commons.io.IOUtils.copy(fileContent, responseOutputStream);
response.flushBuffer();
} catch (IOException e) {
logger.error("Attempt to download file failed", e);
}
}
Yani ben hallederim ve kullanıcı dosyasını indirmek için izin vermeniz gerekir.
$.ajax({
type: "GET",
url: "/download",
data: {
description: "test",
logId: 123
},
success: function(data) {
var blob = new Blob([data]);
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "file.zip";
link.click();
}
})
Kontrolör dosyasını döndürür, ancak o zaman hiçbir şey olmuyor: Müvekkilim tarafı bu içeriyor. Neyi yanlış yapıyorum?
Tamam, ama looong string parametresi almak ve geçmek yerine posta isteği almak istiyorsam ne yapmam gerekiyor? – Everv0id
Bu yöntem, POST için çalışmaz. Ama neden şimdi soruyu değiştiriyorsun? –
Cevabınız, benim özel durumumda mükemmel çalışıyor. Sadece daha derine inmeye çalışıyorum. – Everv0id