0

I have a GET REST API which accepts empno in request and returns the document in response. Right now we are passing the response as ResponseEntity. But when the client hits the URL then this documents gets downloaded from my API call. Below is my sample code:

public class ViewDocController {
    @RequestMapping(value = "/{empno}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<byte[]> viewDoc(@PathVariable(value = "empno") String empno)
            throws ServletException, IOException {      
        return baseBiz.viewDoc(documentRequestBody);
    }
}

But we want that instead of downloading, the file should open in the browser when we hit on the API URL. For example, if in my api url is: http://localhost:8080/rest/services/viewDoc/12345 then on hitting this on explorer, the file should open on the page.

Please suggest any way to do this

Ashutosh
  • 111
  • 14
  • Used "inline" in Header content-disposition and opened the OutputStream to write the bytes. response.setHeader("content-disposition", "inline;filename=" + URLEncoder.encode(filename, "UTF-8")); response.setContentType(contentType.toString()); – Ashutosh Dec 10 '18 at 12:45

1 Answers1

0

You can send back object with byte array like this,

public class ExistingFileDAO extends BaseMessage {

    String fileName;
    byte[] targetArray;

    public ExistingFileDAO(String fileName, byte[] targetArray) {
        this.fileName = fileName;
        this.targetArray = targetArray;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public byte[] getTargetArray() {
        return targetArray;
    }

    public void setTargetArray(byte[] targetArray) {
        this.targetArray = targetArray;
    }
}
WGSSAMINTHA
  • 180
  • 1
  • 11
  • After getting byte array. you can follow below link to display image, https://stackoverflow.com/questions/20756042/javascript-how-to-display-image-from-byte-array-using-javascript-or-servlet – WGSSAMINTHA Dec 06 '18 at 10:27
  • I cant make any changes in the front end side. There is some other api in php which can send the file back directly so that it opens in the browser directly. I need to implement the same way. – Ashutosh Dec 07 '18 at 02:43