- https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Sending_and_Receiving_Binary_Data
- https://github.com/eligrey/FileSaver.js/
- Why threre is no way to download file using ajax request?
- Using jquery ajax to download a binary file
- https://stackoverflow.com/questions/16086162/handle-file-download-from-ajax-post
- https://stackoverflow.com/questions/38975718/how-to-download-excel-xls-file-from-api-in-postman
- http://www.freeformatter.com/mime-types-list.html
- Downloading a file from spring controllers
- https://github.com/hellokoding/uploadingfiles-springboot
Last active
June 15, 2017 07:05
-
-
Save xx7y7xx/9632138c49c94b38b2c632c40990f6f8 to your computer and use it in GitHub Desktop.
Send POST request open save as dialog to save file locally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var oReq = new XMLHttpRequest(); | |
oReq.open("POST", "/download2", true); | |
oReq.responseType = "arraybuffer"; | |
oReq.onload = function(oEvent) { | |
var blob = new Blob([oReq.response], {type: "image/jpeg"}); | |
saveAs(blob, "test.jpg"); // https://github.com/eligrey/FileSaver.js/ | |
}; | |
oReq.send(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Copy from https://github.com/hellokoding/uploadingfiles-springboot | |
*/ | |
package com.hellokoding.uploadingfiles; | |
import org.apache.tomcat.util.http.fileupload.IOUtils; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.ui.Model; | |
import org.springframework.web.bind.annotation.PathVariable; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
import org.springframework.web.bind.annotation.RequestMethod; | |
import org.springframework.web.bind.annotation.RequestParam; | |
import org.springframework.web.multipart.MultipartFile; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.nio.file.Files; | |
import javax.servlet.http.HttpServletResponse; | |
@Controller | |
public class UploadingController { | |
public static final String uploadingdir = System.getProperty("user.dir") + "/uploadingdir/"; | |
@RequestMapping("/") | |
public String uploading(Model model) { | |
File file = new File(uploadingdir); | |
model.addAttribute("files", file.listFiles()); | |
return "uploading"; | |
} | |
@RequestMapping(value = "/", method = RequestMethod.POST) | |
public String uploadingPost(@RequestParam("uploadingFiles") MultipartFile[] uploadingFiles) throws IOException { | |
for(MultipartFile uploadedFile : uploadingFiles) { | |
File file = new File(uploadingdir + uploadedFile.getOriginalFilename()); | |
uploadedFile.transferTo(file); | |
} | |
return "redirect:/"; | |
} | |
@RequestMapping(value = "/download", method = RequestMethod.POST) | |
public File downloadPost() throws IOException { | |
File file = new File(uploadingdir + "test.jpg"); | |
return file; | |
} | |
@RequestMapping(value = "/download2", method = RequestMethod.POST) | |
public void getFile( | |
HttpServletResponse response) { | |
try { | |
// get your file as InputStream | |
InputStream is = new FileInputStream(uploadingdir + "test.jpg"); | |
// copy it to response's OutputStream | |
IOUtils.copy(is, response.getOutputStream()); | |
response.setContentType("image/jpeg"); | |
response.setHeader("Content-Disposition", "attachment; filename=\"somefile.jpg\""); | |
// response.flushBuffer(); | |
} catch (IOException ex) { | |
// log.info("Error writing file to output stream. Filename was '{}'", fileName, ex); | |
throw new RuntimeException("IOError writing file to output stream"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment