greenz1

greenz1

Best way to send multiple files as HTTP response

I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending multiple files. I have tried send_file/5 but it can only send single files at a time.

Should I make these downloads independent or send all the files in bulk? Downloading in bulk helps me reduce the number of HTTP requests.

Marked As Solved

hubertlepicki

hubertlepicki

So that’s not precisely true, I mean it’s precise historically speaking but you have slightly more options nowadays with modern browsers. I’m not saying it’s good or easy options but there are.

One option would be to download the files to the browser’s memory with say JavaScript fetch API Using the Fetch API - Web APIs | MDN . Once you have the file in memory you can then save the file to the filesystem with file system JavaScript API (example: https://jsfiddle.net/koldev/cW7W5/).

One could approach this by concatenating the files server side, with some boundary marker in between, then download the files to browser memory, split it by boundary marker into multiple Blobs, then for each Blob trigger saving to filesystem.

I think at this point browsers (at least Chrome) would actually warn user that “this web site is trying to download multiple files” within a top bar pop up but user can still allow it.

Overall, I do not think it’s worth it. Just do multiple HTTP calls from web site to the back-end, or use ZIP to package multiple files into one.

Also Liked

sarat1669

sarat1669

I don’t think you can send multiple files over a HTTP request. One option would be to add them to a zip.

greenz1

greenz1

Thanks @hubertlepicki and @sarat1669

I think it won’t hurt getting all the images one at a time. JS’ Fetch API requires me to exercise extra caution. and might lead to bizarre errors.

I will go with multiple HTTP calls

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44265 214
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement