0

My webpage provides functionallity to convert pdf to image.

For Webpage i am using Firebase Hosting and for functions obvs Functions.

But after file upload function logs error in firebase dashboard Boundary not found

Below is the code i used to upload file in html:

function uploadFile() {
          var file = document.getElementById("file_input").files[0];
          var pass = document.getElementById("pass").value;
          console.log(file + pass);
          var formdata = new FormData();
          formdata.append("file", file);
          formdata.append("password", pass);
          var ajax = new XMLHttpRequest();
          ajax.upload.addEventListener("progress", progressHandler, false);
          ajax.addEventListener("load", completeHandler, false);
          ajax.addEventListener("error", errorHandler, false);
          ajax.addEventListener("abort", abortHandler, false);
          ajax.open("POST", "/upload");
          ajax.setRequestHeader("Content-Type", "multipart/form-data");
          ajax.send(formdata);
        }
     

and this is the code of functions:

var functions = require('firebase-functions');
var process;
var Busboy;
var path = require('path');
var os = require('os');
var fs = require('fs');

exports.upload = functions.https.onRequest((req, res) => {
  const busboy = new Busboy({ headers: req.headers });
  const fields = {};
  const tmpdir = os.tmpdir();
  const uploads = {};
  const fileWrites = [];
  var pass = '';

  busboy.on('file', (fieldname, file, filename) => {

    console.log(`Processed file ${filename}`);
    const filepath = path.join(tmpdir, filename);
    uploads[fieldname] = filepath;

    const writeStream = fs.createWriteStream(filepath);
    file.pipe(writeStream);

    const promise = new Promise((resolve, reject) => {
      file.on('end', () => {
        writeStream.end();
      });
      writeStream.on('finish', resolve);
      writeStream.on('error', reject);
    });
    fileWrites.push(promise);
  });

  busboy.on('field', function (fieldname, val, fieldnameTruncated, valTruncated, encoding, mimetype) {
    pass = val;
  });

  busboy.on('finish', function () {
    console.log('Done parsing form!');
    console.log(pass);
    console.log(uploads);

    process.processCard(uploads['file'], pass, 2).then((s) => {
      res.end(`
        <!DOCTYPE html>
        <html>
           <body>
              ImageConverted!!
              <img src="data:image/jpeg;base64,${s}" width="90%"></img> 
           </body>
        </html>
        `);
    }).catch((err) => { res.end('Error: ' + err) });
  });
  busboy.end(req.body);
}); 

What am i doing wrong ?

jecol
  • 21
  • 7
  • Can you try setting the boundary manually? Like `ajax.setRequestHeader("Content-Type","multipart/form-data; charset=utf-8; boundary=" + Math.random().toString().substr(2));` – BEAGLE Aug 11 '20 at 07:31
  • @BEAGLE i tried but then no file is sent i think as in logs i see no file. NO BOUNDRY ERROR is gone but the file is not there. so i guess it didn't worked for me. – jecol Aug 11 '20 at 08:27

1 Answers1

0

For multipart body it is recommended to use req.rawBody instead of req.body

https://stackoverflow.com/a/48289899/6003934

Juancki
  • 1,793
  • 1
  • 14
  • 21
  • Hey! Thank you very much for the reply. I think the problem is in the html code where i do the ajax upload. and the req.body was the typo i am using the `rawBody`. – jecol Aug 15 '20 at 16:24
  • 1
    Did you manage to fix the issue by using `req.rawBody` instead? If so, can you please post the final result as an answer, so other users who might run into similar issues can benefit from this question? You may also consider upvoting and marking this answer as accepted if you found it helpful. Thanks @jecol – sllopis Aug 17 '20 at 08:57