Meteor-files: How to get link file after upload in Meteor Files

Created on 4 Nov 2020  路  10Comments  路  Source: veliovgroup/Meteor-Files

Hi, I am uploading my text editor (Quil Editor) files using React and Meteor-Files.
But I do not know exactly how to get the link after uploading and put it in the editor.
Do I have to subscribe to get the link to that file after upload?
And if so. Is there a way to subscribe to that file right away?
Note that I have to subscribe to this file in such a way that I can get the file link in the same method (UploadImage)

Because if I subscribe to it as a withTracker, this method will not be rendered again so that I can get the link to that file.

I asked here too and sied more :
https://forums.meteor.com/t/how-to-get-link-file-after-upload-in-meteor-files/54432

question

Most helpful comment

@dr-dimitru
It was great, he answered.
Thank you for your prompt and accurate response

All 10 comments

Hello @Saeed-Bahrami ,

  1. Subscribing is one of the options;
  2. No, it's optional

I updated .link() method documentation, hope now this method usage will be more straightforward.

TL;TR; You have next options:

  1. After upload on server and client hooks you can get fileRef the copy of the file object from MongoDB record; Using this record you can do FilesCollection#link(fileRef)
  2. __Not the best option__ other way is to subscribe and use FilesCollection#find().link()

Feel free to close it in case if the issue is solved on your end.

@dr-dimitru
I'm sorry, but I still did not understand correctly.
As I realized, the following code should work, and without subscribing, I should get the link after uploading.

 const upload = ImgCollection.insert({
           file: Img,
           streams: 'dynamic',
           chunkSize: 'dynamic'
 }, false);

console.log(upload);

upload.start();

const file = ImgCollection.findOne({_id:`${upload.config.fileId}`});
console.log(file.link());

As I realized, the following code should work, and without subscribing, I should get the link after uploading.

  1. __Yes, on server__
  2. __No, on client__

Listen for end event on the Client as in the example link from my message above. Something like that:

 const upload = ImgCollection.insert({
  file: img
}, false).on('end', function (error, fileObj) {
  // get link in here
  console.log(ImgCollection.link(fileObj))
}).start();
upload.start();

@dr-dimitru
It was great, he answered.
Thank you for your prompt and accurate response

It was great, he answered.

yeah, he answered kk

@Saeed-Bahrami I'm glad this issue was quickly solved

Please support this project with:

@dr-dimitru Hello again.
I got the image link the same way, and save it in the database so I can display it later.
But after a while (I do not know exactly how many hours) the links expire and error 404 returns.
Do the links expire?
What method should we use to always display images?

@Saeeed-B it sounds to me that you ran into different issue described in our FAQ:

  • Note: __All files will be removed as soon as your application rebuilds or you run meteor reset.__ To keep your storage persistent during development use an absolute path outside of your project folder, e.g. /data directory.

If it's not the case, I'll request more details

@Saeeed-B is this one solved o you end? You haven't replied since my comment

@dr-dimitru Hi, thank you for your help, I have to test Rahel.
I will definitely report if I see a problem
Thanks

Was this page helpful?
0 / 5 - 0 ratings

Related issues

schlaegerz picture schlaegerz  路  3Comments

owenhoskins picture owenhoskins  路  3Comments

sidkdbl07 picture sidkdbl07  路  4Comments

dr-dimitru picture dr-dimitru  路  3Comments

ck23onGithub picture ck23onGithub  路  3Comments