T
T
Taras Labiak2017-03-22 18:06:44
Node.js
Taras Labiak, 2017-03-22 18:06:44

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
          }
        })
    }
  }
}

And it used to work (as far as I remember), but now the file size is zero. The code was written when Node.js 6.* was installed, now Node.js v7.7.3 As a result I get the correct answer, but the downloaded file has a zero size
{  
   "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"
}

It uses its own framework, which extends express.js

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Ltonid, 2017-04-01
@AtaZ

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 question

Ask a Question

731 491 924 answers to any question