How could I specify the upload file server path via js api or rest api?

Hello,

I am new and studying the Files for parse server, just wondering the uploaded file path, is there any way I could specify them via js api or rest api?

For example, I designed an image folder at /usr/local/parse-server-example/public/images, and I want to save all the images there (and subfolders by some naming rules, like images/20211013/timestamp1.jpg, images/20211014/timestamp2.jpg, ) which are uploaded from client side, how could I accomplish this via js api?

Thanks a lot.

Thats not possible with parse server. Currently files are saved to database or other storage providers such as Amazon or DigitalOcean. If you want to save files to local folders, you need to add your custom express route.

Edit: Seems like my statement is not true. See cbaker6’s message.

Thanks so much. If using custom express route, it is no possible to use the parse client or server side SDK then?

If you really need Parse JS SDK or want to use it, maybe you can create a cloud function and pass file to parameters as base64 string.

Parse.Cloud.define('uploadFile', async request => {
  const { base64, fileName } = request;
  return await new Promise((resolve, reject) => {
    fs.writeFile(`images/20211013/${fileName}`, base64, 'base64', err => {
      if (err) reject(err);
      else resolve(true);
    });
  });
});

You can store to the file system using the Parse FS adapter which is the recommended way:

Be sure to look through the readme for instructions.

You can also see more in the official documentation:

Then you just use ParseFile as you would with the default adapter and everything is handled automatically by the server without cloud code

2 Likes

@cbaker6 @uzaysan Thank you so much!

Hi @uzaysan ,

Just got time to try your solution, while got below error:

I have already simplified the cloud function as below, many lines have been commented out, while still met above error. Is there anything missing? Looks like Parse Server met error when parsing form-data. Thanks a lot.

Parse.Cloud.define(‘uploadFile’, async request => {
// console.log(‘uploadFile.request’, request);

// const { base64, fileName } = request;
// console.log(‘uploadFile.fileName=’ + fileName);
return ‘ok’;
// return await new Promise((resolve, reject) => {
// fs.writeFile(images/20211230/${fileName}, base64, ‘base64’, err => {
// if (err) {
// reject(err);
// } else {
// resolve(true);
// }
// });
// });
});

you need to convert file to b ase64 string and pass that as a parameter. It wont work if you pass file directly as request body.