3

I've been building a manual multipart request with Javascript. I would use FormData but it is not stringifable and so I cannot hash it.

I've got the request down but I can't seem to read in the file correctly. I have a hunch it is an encoding issue.

As soon as I read my file in it seems it is corrupted or broken by converting it to a string. The file I've been testing is a PDF.

The original file looks like this:

%PDF-1.3
%ƒÂÚÂÎßÛ†–ƒ∆
5 0 obj
<< /Length 6 0 R /Filter /FlateDecode >>
stream
x•X…nG

When reading the file in with:

reader.onload = arrBufferCallback;
reader.readAsBinaryString(blob);

The output is:

%PDF-1.3
%Äåòåë§ó ÐÄÆ
5 0 obj
<< /Length 6 0 R /Filter /FlateDecode >>
stream
x¥XÉnG½÷WԨܬ­»@AÜCC¬H

Which is just plain wrong.

I've tried half a dozen other options like:

var bytes = new Uint8Array(e.target.result);
resolve(String.fromCharCode.apply(null, bytes));

Trying to use a TextDecoder comes out worse.

All I need is the original file contents in a string form?

Malii
  • 448
  • 6
  • 17
  • This may interest you [fileReader.readAsBinaryString to upload files](https://stackoverflow.com/a/17512132/387194) – jcubic Aug 13 '19 at 15:17
  • The jist of it is that you can't generate a digest header for a multipart request in Javascript? (Because we can't read a file as a string and thus can't hash it) – Malii Aug 27 '19 at 13:30

0 Answers0