Anshika pandey
Anshika pandey

Reputation: 1

could not open file after downloading

I could download files the API but after download the file doesnt open. It shows either corrupted or sent as an attachment but same file after downloading from POSTMAN response works. I am using java to hit api

public FileResponseModel downloadFileByPathHandler(ExtractPageDataQuery query) throws Exception {

    InstanceInfo instanceInfo = ((InstanceInfo) this.getServiceInfoFromEureka.apply(this.client, serviceName));
    URL baseUrl = this.getBaseUrl.apply(instanceInfo);
    WebClient webClient = WebClient.builder().baseUrl(baseUrl.toString()).build();

    ByteArrayResource resource = webClient.post().uri("/" + serviceName + "/file-management/download")
            .headers(this::setRequestHeaders)
            .contentType(MediaType.APPLICATION_JSON)
            .body(BodyInserters.fromValue(query))
            .retrieve()
            .bodyToMono(ByteArrayResource.class)
            .block();
    byte[] fileBytes = resource.getByteArray();
    ContentDisposition disposition = ContentDisposition.builder("attachment")
            .filename("downloadedFile.pdf")
            .build();
    MediaType mediaType = MediaType.APPLICATION_OCTET_STREAM;
    FileResponseModel fileResponseModel = FileResponseModel.builder()
            .mediaType(mediaType)
            .bytesArray(fileBytes)
            .message("File successfully downloaded")
            .sendFileInResponse(true)
            .disposition(disposition)
            .build();
    return fileResponseModel;

}

is it FE error or BE error

Upvotes: 0

Views: 59

Answers (0)

Related Questions