javascript - Uploading mp3 to NodeJS Express server corrupts file -
i upload mp3 file web client node express server. can upload file, write disk. however, file gets corrupted , cannot play song using mp3 player.
if makes difference, file being uploaded os x machine, gets written ubuntu server. size of uploaded of uploaded file larger original. receive no errors in workflow. nothing goes wrong until start play file.
please see below code. ideas why i'm experiencing this?
client
uploadsong: (song) => { fetch('http://localhost:8080/song', { method: 'put', headers: { 'content-type': 'audio/mpeg', }, body: song }) .then( response => { console.log("got response after uploading song:", response); }) .catch( error => { console.log("error in firebase ac upload song: ", error); }); }
server
app.put("/song", (req, res) => { var mp3songname = 'test.mp3'; var mp3_file = fs.createwritestream(mp3songname); mp3_file.on('open', function (fd) { req.on('data', function(data){ console.log("loading... \n"); mp3_file.write(data); }); req.on('end', function(){ console.log("finalizing..."); mp3_file.end(); res.sendstatus(200); }); }); }
i noticed when inspecting uploaded files data getting written base64 encoded string.
i traced client code, using filereader.readasdataurl method read file:
...the result attribute contains data url representing file's data base64 encoded string
mystery solved. need figure out whether decode file on server, or use filereader method doesn't encode in first place.
Comments
Post a Comment