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

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

We're in Beta

About us Mission Statement