Access disk data

Hi everyone,

I’m just starting with Render.
I have a disk and i can upload a file on it. The file is also on a database.
However, i can’t get the files.

Here are the requests i use, to upload a file and to get a file.

Thanks for your help! :slight_smile:

//Upload

const mountPath = '/var/data';
exports.upload = async (req, res) => {
  let id = Math.floor((1 + Math.random()) * 0x100000000000)
    .toString(16)
    .substring(1);
  
  if (!req.files || Object.keys(req.files).length === 0) {
    return res.status(400).send('No files were uploaded.');
  } 

  const sampleFile = req.files.file;  
  const destinationPath = path.join(mountPath, sampleFile.name);
 
  sampleFile.mv(destinationPath, (err) => {
    if (err) {
      console.error('File upload error: ' + err);
      return res.status(500).send('File upload error: ' + err);
    }

    db.query(
      `INSERT INTO files (id, user_id, name, type, size, content, uploaded_at) VALUES (?, ?, ?, ?, ?, ?, NOW())`,
      [id, req.userData.userId, sampleFile.name, sampleFile.mimetype, sampleFile.size, sampleFile.content],
      (dbErr, result) => {
        if (dbErr) {
          console.error('Database insertion error: ' + dbErr);
          return res.status(500).send('Database insertion error: ' + dbErr);
        }       
        res.send('File uploaded and saved to the disk!');
      }
    );
  });
};

//Get file

exports.getFile = (req, res, next) => {
  const graphId = req.params.graphId;
  const filePath = path.join('/var/data/', graphId) 
  if (fs.existsSync(filePath)) {   
    res.sendFile(filePath, (err) => {
      if (err) {
        console.error(err);
        return res.status(500).send('Error while sending the file');
      }
    });
  } else {   
    res.status(404).send('File not found');
  }
};

Hi there,

What error do you see when trying to download the file? You can use the shell tab on our dashboard to inspect what files you have in /var/data. This will allow you to ensure you are passing the correct value for graphId, which would need to be the filename.

Regards,

Keith
Render Support, UTC+10 :australia:

Hi Keith,

So you saved me…, thanks !
The problem was that i was trying to get the file with its id, and not by its name.

Thanks again!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.