Handling excel file with FormData in Angular and import...

Discussão em 'Angular' iniciado por Chandru, Abril 15, 2025.

  1. Chandru

    Chandru Guest

    Angular: Uploading an excel file from Angular with FormData:

    //On upload event, defining formData:
    public onUpload(event: any): void {
    this.uploading = true;
    const selectedFile = event.files[0];
    this.fileFormData.append('file', selectedFile, selectedFile.name);
    this.uploadedFiles.set(null, new File([""], selectedFile.name));
    this.uploading = false;
    this.showCompletedMessage();
    }

    //And sending formData on submit in Post request:
    upload(formData: FormData): Observable<any> {
    const url = `${this.ITEM_CHANGE_REQUEST_URL}/import-manual-usage-list`;
    return this.http.post<any>(this.util.getServerBaseUrlNgRest() + url, formData, {
    headers: new HttpHeaders({ 'content-type': 'multipart/form-data' })
    });
    }



    Backend Handling the request with import org.glassfish.jersey.media.multipart.FormDataParam;

    @POST
    @Path("/upload-file")
    public Response importFile(@FormDataParam("file") InputStream fileInputStream) {
    try {
    java.nio.file.Path path = java.nio.file.Path.of("D://Temp");
    path.resolve("filename.xlsx");
    Files.copy(fileInputStream, path);
    Response.ok(true).build();
    } catch (Exception e) {
    throw new InternalServerErrorException("error", e);
    }
    return Response.ok().build();
    }


    Trying the create an excel file from InputStream, file has been created with same KB as uploaded file, but it's not recognizing as excel file. Upon opening the file, no data in the sheet.

    Pls suggest how to approach and solve this issue?

    Continue reading...

Compartilhe esta Página