@Description("MetaDataFile Download from Admin.")
@GetMapping
@PreAuthorize("hasRole('ADMIN')")
@RequestMapping("/api/admin/apiCallLog/{apiCallLogId}/file")
public ResponseEntity<?> getMetaDataFile(@CurrentUser UserPrincipal currentUser,
@PathVariable Long apiCallLogId) {
Map<String, Object> resultMap = new HashMap<String, Object>();
URL url = new URL(...어찌어찌해서 얻어옴);
InputStreamResource resource = new InputStreamResource(url.openStream());
resultMap.put("fileName", apiCallInfo.getFileName());
resultMap.put("resource", resource);
return ResponseEntity.ok().contentType(MediaType.parseMediaType("application/octet-stream"))
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachement; filename=\"" + resultMap.get("fileName") + "\"")
.body(resultMap.get("resource"));
}
구현된 API call하는 부분
export async function getFileDownload(requestPath, requestMethod, rowData) {
await fetch(API_BASE_URL + requestPath, {
method: 'GET',
headers: getHeaders(),
})
.then(response => {
return response.blob();
})
.then(blob => {
const url = window.URL.createObjectURL(
new Blob([blob]),
);
const link = document.createElement('a');
link.href = url;
link.setAttribute(
'download',
rowData.fileName,
);//filename to be downloaded, it can be used as link.download=fileName
//link.style='display:none'; //OK it is not given
document.body.appendChild(link);
link.click();
link.parentNode.removeChild(link);
})
.catch(err => {
alert('error')
});
}
let img = new Image()
img.crossOrigin = "Anonymous"; // Helps with some cross-origin issues
fetch('foo.png')
.then(response => response.blob())
.then(blob => { img.src = URL.createObjectURL(blob); })
위 방법써도 아래와 같은 CORS 에러발생
Access to fetch at 'http:/..jpg' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.