#[SpringBoot + Kotlin + WebFlux] cannot fetch data in the frontend

1 messages · Page 1 of 1 (latest)

vapid rivet
#

hey, I meet a problem when using spring-boot-starter-webflux, here is my code relevant
in controller,

@GetMapping("/download")
suspend fun downloadFile(@RequestBody request: DownloadRequest,
                         response: ServerHttpResponse) {
    val databuffer = response.bufferFactory().allocateBuffer(bufferSize)
    val outputStream = databuffer.asOutputStream()

    // outputStream.write here
    sambaUtils.downloadFile(request.path, request.filename, outputStream)
}

I have logged the writing bytes,

frosty larkBOT
#

<@&1008423204219531294> please have a look, thanks.

frosty larkBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

oad")
fun download(): Mono<ResponseEntity<Resource>> {
val file = File("path/to/file")
val resource = FileSystemResource(file)

return Mono.just(ResponseEntity.ok()
        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=${file.name}")
        .contentType(MediaType.APPLICATION_OCTET_STREAM)
        .body(resource))

}


in frontend,

```javascript
fetch('/download')
  .then(response => response.blob())
  .then(blob => {
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.style.display = 'none';
    a.href = url;
    a.download = 'file.txt';
    document.body.appendChild(a);
    a.click();
    window.URL.revokeObjectURL(url);
  });

but it seems that the frontend cannot fetch the data correctly, what's wrong with my code?