0

I'm making a sending file function of a chatroom. The language I'm using is nodejs (socket.io) and js.

The way I work it out is to

  1. use javacript fileReader to read a file in chrunks as ArrayBuffer, and transfer the pieces to server, server collects the data until all data is collected.

  2. Sever send ArrayBuffer back to the other clients.

  3. On the clientside, convert ArrayBuffer to base64String, and add prefix depends on the MIME type such as data:application/pdf;base64,.

  4. Insert this base64String to a <a href='{thisBase64String}' download></a>.

Currently, it only works on those pdf files whose size are less than 2MB, the download attribute doesn't work on fileSize over 10MB.

My guess is the size of href too long causing the problem, any idea to fix this problem?

I know probably I can use fs.write to print out the uploaded file first, then link to the directory. But since it is a chatroom, I wish I can make everything on the air. There is no need spending time to maintain the server, isn't it?

Janice Zhong
  • 836
  • 7
  • 16
  • It'd be better to place that file somewhere in public directory of your web server ( save it with node when you get all chunks from client ) , and then just send them a short link – Marko Mackic Sep 16 '18 at 04:37
  • @MarkoMackic Thank you for your suggestion, though I mentioned this advice in the question already. This problem can be answered by 'Data protocol URL size limitations', so I can do nothing about the URL limitations, which makes your advice the only way that can work for me. – Janice Zhong Sep 16 '18 at 05:06
  • The previous dupe target was not related to your issue whatsoever. I changed it to two other answers that will give you a real solution: don't use a fileReader at all, create simply a blobURI from your Blob, and set your href to this blobURI. No size limit now. – Kaiido Sep 16 '18 at 06:10
  • Hi @Kaiido, that was a very smart solution, appreciate that! Just that I don't understand why do we need to `requestAnimationFrame()` before `revokeURL()` in your answer of [Is it possible to programmatically detect size limit for data url?](https://stackoverflow.com/questions/36918075/is-it-possible-to-programmatically-detect-size-limit-for-data-url) – Janice Zhong Sep 16 '18 at 08:50
  • @Kaiido Can we also use a similar solution to generate a objectURL href for pdf? – Janice Zhong Sep 16 '18 at 08:53
  • Oh the rAF was just a way to ensure there has been enough tome for the download to begin. But that can probably be done in better ways actually. – Kaiido Sep 16 '18 at 09:02

0 Answers0