expressjs / expressjs/multer

File size is 0kB after successfully uploading the file

Open
#1,019 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12.1k
Forks
1.1k
Avg merge
8d 2h
Merged PRs (30d)
21

Description

Hi guys I know this question may a silly one, but have you encounter this problem before? may you share your experience to handle this problem?

So my API retrieves an image from Telegram bot, to save our time, let's say that I successfully got the file from Telegram API (Its true) and store the file inside the API directory. I have one endpoint to upload image using multer that working correctly and tested using postman. this endpoint retrieves the file using form-data structure. But when I try to upload the existing image on the API directory to the upload endpoint, the endpoint sent 200 but when I check on the upload folder, the file size is 0.

Here's I attached my code to clearly explain the problem I encountering
BTW, I am using fs.createReadStream to get the existing image

```
// telegram-bot-logic.js
let file = await axios.get(fileLink);
let fileName = crypto.randomBytes(20).toString('hex') + ".jpg"
const downloadStream = got.stream(file.config.url);
const fileWriterStream = fs.createWriteStream(fileName);

downloadStream
.on("downloadProgress", ({ transferred, total, percent }) => {
const percentage = Math.round(percent * 100);
ctx.reply(`progress: ${transferred}/${total} (${percentage}%)`);
})
.on("error", (error) => {
console.error(`Download failed: ${error.message}`);
});

fileWriterStream
.on("error", (error) => {
console.error(`Could not write file to system: ${error.message}`);
})
.on("finish", () => {
console.log(`File downloaded to ${fileName}`);
});
downloadStream.pipe(fileWriterStream);

let fileToUpload = fs.createReadStream(process.cwd() + `/${fileName}`);
fileToUpload.on('close', () => {
let form = new FormData();
form.append('profile_avatar', fileToUpload);

botEndpoint
.postProfilePicture(userDTO, form)
.then((res) => {
ctx.reply(res.data.data.message);
return ctx.scene.enter("meat-menu");
})
.catch((err) => {
ctx.reply("there was a problem when processing the image that you sent");
console.error(err);
return ctx.scene.leave();
})
})
```

```
// multer.js
const multer = require('multer');
const fs = require('fs');

exports.upload = multer({
storage : multer.diskStorage({
destination : function (req, file, cb) {
let destination = req.url.split('/');
let customPath = './uploads/' + destination[2] + "/";
fs.mkdirSync(customPath, { recursive : true });
cb(null, customPath);
},
filename : function (req, file, cb) {
let filename = req.user.id + "-" + file.fieldname + "-" + file.originalname.toLowerCase();
req.body.filename = filename;
cb(null, filename);
}
})
});
```

```
// route.js
app.post('/api/profile/upload/avatar', auth, upload.single('profile_avatar'), addProfilePicture);
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.