Answer the question
In order to leave comments, you need to log in
Why can't I upload a file?
I can't figure out why the uploaded file gets zero size. Here is the code that downloads the file and writes it to the database
function upload(req, res) {
if (req.user.guest) {
res
.status({status: 401})
.json(401)
}
else {
const mime = req.headers['content-type']
const size = +req.headers['content-length']
const type = req.config.file.mime[mime]
if (type && size) {
if (size <= type.size) {
const now = Date.now()
const id = now.toString(36) + req.user.id.toString(36)
const filename = id + '.' + type.ext[0]
const tempfilename = path.join(os.tmpdir(), filename)
const temp = fs.createWriteStream(tempfilename)
temp.on('error', res.error)
temp.on('close', function () {
fs.rename(tempfilename, req.config.file.dir + '/' + filename, function (err) {
if (err) {
res.error(err)
}
else {
const data = {
id,
mime,
size,
ip: req.headers.ip,
name: req.params.name,
user: req.user.id
}
res.promise(req.table('file').insert(data)
.then(function () {
data.status = 201
data.success = true
data.url = '/files/' + filename
return data
}))
}
})
})
req.pipe(temp)
}
else {
res
.status(413)
.json({
status: 413,
error: {
message: `Request entity ${mime} too large: ` + size
}
})
}
}
else {
res
.status(415)
.json({
status: 415,
error: {
message: 'Unsupported media type: ' + type
}
})
}
}
}
{
"id":"j0l3hagb2",
"mime":"image/jpeg",
"size":221518,
"ip":"127.0.0.1",
"name":"stock-photo-142984111-1500x1000.jpg",
"user":2,
"status":201,
"success":true,
"url":"/files/j0l3hagb2.jpeg"
}
Answer the question
In order to leave comments, you need to log in
Check the write permissions to the tempo folder and to the final folder. The correct answer says that the file was created in the temp folder, but it was not possible to read and transfer it to the combat folder. Maybe the place is over.
You can also add an output of the size of the temp file.
I remembered, it still depends on the owner of the folders, set the rights to 777 if it works with them and not with 755. means the owner's problem.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question