Sending binary data through JavaScript problem

I found an interesting point in Dealing with binary data part of Sending forms through JavaScript article.

Code from this part’s example send files to server without any problems, but i can’t open images after sending to server.

For playback i use simple Express server:

const express = require('express');
const app = express();
const cors = require('cors');
const multer  = require('multer');

const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, __dirname + '/files/')
  },
  filename: (req, file, cb) => {
    const uniqueSuffix = Date.now();
    cb(null, uniqueSuffix + file.originalname);
  }
});

const upload = multer({ storage: storage });

app.use(cors({
  origin: '*'
}));
app.use(express.raw());
app.use(express.urlencoded({ extended: true }));

app.post('/', upload.single('myFile'), (req,res) => {
  console.log(req.body, req.file);
  res.send('Заходите!');
});

app.get('/', (req, res) => {
  res.send({ message: 'Hello WWW!' });
});

app.listen(3333);

And send to localhost:3333 POST request with image’s binary, but image broke after upload.
image

So, if i send image with XMLHttpRequest and FormData or just simple form like this:

Everything is ok.

Github and problem example.

Hi @VladimirK

I don’t know about this matter to give you an explanation, but you could try comparing the headers and the body of the two variant with the DevTools network tab. Maybe you find some differences that explain the situation.

Stay safe,
Michael

1 Like