[FIXED] How to use PrimeNG p-fileUpload without multipart/form-data?

Issue

I’m using p-fileUpload to upload a .zip file into AWS S3, the upload works and I can download and open the file correctly by the OS application, but when I tried to read the same file using an InputStream Java, it doesn’t work so well, e.g.

try (InputStream in = new URL(uri).openStream()) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IOUtils.copy(in, out);

        return ByteBuffer.wrap(out.toByteArray());
    } catch (IOException ex) {
        throw new SomeException(ex);
    }

Inspecting the file I see the following data:

------WebKitFormBoundaryhrGInEllRBnFuzls
Content-Disposition: form-data; name="MyAwesomeFunction970"; filename="hello-lambda.zip"
Content-Type: application/zip

PK
�0E��w�DA����D:5t�)��
�w�l����\1�)bvs۠�)�ĺ���>DYa#f��&��U�p�mC�GS�i��<n���$�p3ux����

------WebKitFormBoundaryhrGInEllRBnFuzls--

The data above has some headers information and things that seems to be part of a multipart/form-data upload.

If I put the file through Postman, for example, the data is a little different (without the headers) and then works well via InputStream Java code.

PK
�0E��w�DA����D:5t�)��
�w�l����\1�)bvs۠�)�ĺ���>DYa#f��&��U�p�mC�GS�i��<n���$�p3ux����

Anyone knows what’s happening with the upload process and how to fix it?

Solution

I figured out this problem by calling a custom upload method, e.g:

<p-fileUpload
    name="{{uploadFileName}}"
    url="{{uploadUrl}}"
    customUpload="true"
    (uploadHandler)="customUpload($event.files, uploadUrl)">
</p-fileUpload>

customUpload(files:any, uploadUrl:string) {
    var reader = new FileReader();
    reader.onload = function (evt) {
        var xhr = new XMLHttpRequest();
        xhr.open('PUT', uploadUrl, true);
        xhr.setRequestHeader('Content-Type', files[0].type);
        xhr.send(evt.target.result);
    };
    reader.readAsArrayBuffer(files[0]);
}

Answered By – Luan Kevin Ferreira

Answer Checked By – Timothy Miller (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published