0

I'm currently working on an application that allows the user to upload a file which gets sent to a express server that then converts that file into a bytearray that I can then store somewhere else.

However, I need to be able to convert this bytearray back into a file and send it back to the user. This is my current code in the express API:

app.post("/upload", async (req, res) => {
  const file = req.files.file;
  const filePath = "divali";

  file.mv(filePath, async (err) => {
    const nfile = fs.readFileSync(filePath);
    let fileData = nfile.toString("hex");
    let result = [];
    for (var i = 0; i < fileData.length; i += 2)
      result.push("0x" + fileData[i] + "" + fileData[i + 1]);

    console.log(result);

    var pfile = new Blob([result], { type: "application/pdf" });
    // var fileURL = URL.createObjectURL(pfile);
    console.log(pfile);

    pfile.lastModifiedDate = new Date();
    pfile.name = "some-name";

    console.log(pfile);
  });
});
Jklder
  • 19
  • 1
  • does this help you? https://stackoverflow.com/questions/27159179/how-to-convert-blob-to-file-in-javascript/29390393 – sdotson Apr 09 '20 at 00:10
  • Not really. I'm getting back a null when I use: theBlob.lastModifiedDate = new Date(); theBlob.name = fileName; return theBlob; – Jklder Apr 09 '20 at 00:37
  • the File constructor method too? https://stackoverflow.com/a/31663645/3393804 – sdotson Apr 09 '20 at 00:56
  • 1
    I cannot use the File constructor. It says it's not defined. Is it in some nodejs module I can install? – Jklder Apr 09 '20 at 00:58

0 Answers0