skip to Main Content

I am trying to expose an api which downloads files can be of different types, I am showing excel for an example. The file is successfully getting downloaded with GET method endpoint but when i do the same with POST method it doesn’t download it but i can see contents of the file in network tab as texts. I tried removing all logic and doing it with empty file and it is same. sample code shared. This backend api is getting called from a frontend application page written in REACT.

@GetMapping("/downloadGet")
    public void downloadManualBulkRefund1( DownloadRequest 
     downloadRequest,HttpServletResponse response) throws Exception {

        FormatterOutputTO output = new FormatterOutputTO();

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment; 
    filename="sample_18082023.xlsx"");

        response.getOutputStream().write(output.getByteStream() != null ? 
     output.getByteStream() : new byte[] {});

        response.getOutputStream().flush();
        response.getOutputStream().close();

    }

For Post method everything is same except it is a post method.

2

Answers


  1. What is the log in your backend server saying ?

    Login or Signup to reply.
  2. You have to change your method return type from Void to resource type by mentioning a Generic partof the response entity class. Ref example

    package net.codejava.upload;
     
    import java.io.IOException;
     
    import org.springframework.core.io.Resource;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RestController;
     
    
        @RestController
        public class FileDownloadController {
             
            @GetMapping("/downloadFile/{fileCode}")
            public ResponseEntity<?> downloadFile(@PathVariable("fileCode") String fileCode) {
                FileDownloadUtil downloadUtil = new FileDownloadUtil();
                 
                Resource resource = null;
                try {
                    resource = downloadUtil.getFileAsResource(fileCode);
                } catch (IOException e) {
                    return ResponseEntity.internalServerError().build();
                }
                 
                if (resource == null) {
                    return new ResponseEntity<>("File not found", HttpStatus.NOT_FOUND);
                }
                 
                String contentType = "application/octet-stream";
                String headerValue = "attachment; filename="" + resource.getFilename() + """;
                 
                return ResponseEntity.ok()
                        .contentType(MediaType.parseMediaType(contentType))
                        .header(HttpHeaders.CONTENT_DISPOSITION, headerValue)
                        .body(resource);       
            }
        }
    

    External Ref for more detials : https://www.codejava.net/frameworks/spring-boot/file-download-upload-rest-api-examples

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search