How to parse a txt-file and then send it via multi-part?

103 Views Asked by At

I am trying to change a file, uploaded to form via user, before being sent to server via POST and multipart (I inactivate the button until all file is parsed fully). Is there a way to change the uploaded file ( i call it newFile below) in the form? I have tried things like this:

document.getElementById("myFormInputField").files[0] = newFile;

and change the form data for the file with:

formData.set(name, newFile, fileName)

Should any of that work?...

My solution is instead to use XMLHttpRequest like below. And convert the encoding I have in file from CP473 to UTF8 with readAsArrayBuffer with following: PC8 / CP437 character set with filereader in Chrome

function sendFile(newFile) {

  const uri = "/upload";
  const xhr = new XMLHttpRequest();
  const fd = new FormData();

  xhr.open("POST", uri, true);
  xhr.onreadystatechange = function() {
      if (xhr.readyState == 4 && xhr.status == 200) {
          alert(xhr.responseText); // handle response.
      }
  };
  fd.append('myFile', file);
  // Initiate a multipart/form-data upload
  xhr.send(fd);
}
0

There are 0 best solutions below