OvermindDL1

OvermindDL1

Uploading Multiple Files

So I need users to upload a few hundred to a few thousand tiny image files, obviously doing that picture-by-picture would be extraordinarily painful, so using the multiple option of the standard file input element to allow selecting the entire directories contents.

Now, when doing <input type="file" id="images" name="images"> I get a %Plug.Upload{} structure just fine, however if I do <input type="file" id="images" name="images" multiple> then I get a binary that is the name of one of the listed files, so changing that to <input type="file" id="images" name="images[]" multiple> gets the params giving me a list of binaries of the file names, however these are not %Plug.Upload{} structures, so I don’t know anything beyond the filenames, like say the contents of the files.

I’ve found this past thread here that was useful enough to get me to add the [] part to the name, but don’t actually help me getting to the contents (and if you check the attached github issue they never seemed to handle it either):

Furthermore, using inotifywait on the entirety of the /tmp directory shows there is no file writing happening at all for a multiple file upload, yet single file uploads without the multiple attribute save and are deleted just fine.

The docs of Plug.Upload are not useful on this issue either.

The total size of the files are well well below the max file size (only a couple hundred kb while testing and just trying to get it working).

Considering this seems to be a pretty basic use-case I have to be screwing up pretty big somewhere, thus any assistance would be very welcome! ^.^;

Marked As Solved

OvermindDL1

OvermindDL1

Ah hah! I am the idiot! To forever encode my missing bit into history and maybe help others later, don’t forget the multipart: true option to the form call! I noticed it within seconds of opening the file when I got in today. Yesterday was a loooong day and I missed the obvious. ^.^

13
Post #7

Also Liked

gregvaughn

gregvaughn

I can’t confirm either, but I know Rails uses it, and DHH is from the PHP community, so that’s my assumption of where it comes from.

peerreynders

peerreynders

More importantly - post the entire form fragment when discussing problems with input elements. On MDN enctype="multipart/form-data" kind of sticks out like a sore thumb - but I figured that you had that covered given you got the PHP version working.

Aside: Does anybody have any information on the origin of the “[]” name suffix convention for multiple files? It seems that the HTML specification doesn’t require it but it seems to be more of a server-side convention (PHP specifically). Or am I wrong about this?

Eiji

Eiji

Only for standard HTTP upload. Is not WebSockets standard better for that? You can send multiple files at a time in their own small requests without sending HTTP headers, extra (every request) validation etc. It’s also better to for future changes like showing upload progress, chucking etc. Personally I don’t like also ideas of sending multiple files in one request. Application should work as long as it can. In your case if you would have a network problem in the middle of process then you would need to send again all of hundred or even few thousand files. Even if they are tiny it’s not good for user experience and it’s also not scalable solution. Remember that there are still people which do not have fast fiber internet. :smiley:

I believe that @josevalim should know best this problem as he is most active contributor to plug. I think that you should create a GitHub issue for this problem in elixir-plug/plug. I did not looked at source code of plug, but if I would guess then I think that plug is trying to handle multiple upload as same as normal upload which means that it hits an edge case of file upload implementation in plug where developers does not expect to parse request content of multiple files. Also I believe that Issue when uploading a file with multipart/form-data with Chrome but trying to upload a directory instead of a file plug issue is really similar to your case (not properly handled request content standard). Again I recommend to create new issue with providing all data as in linked issue. Also if you would look on that issue then you would see that different browsers could send requests content in different format, so make sure to test them and give us information about it as in linked issue.

Yeah, just found that @josevalim commented about it 2 years ago:

It doesn’t seen the multiple option is working for file_input. So you need to do rather something like: file_input f, :files, name: "files[]" or send a PR so we support multiple. :slight_smile:

This means that you need to have multiple file_input with name which ends with [] (because it shows Phoenix that we want to save value of multiple inputs into one list). [] suffix should not change parsing parameters for only one field. Just instead of value you would have [value] and nothing more, so it’s useless for you.

Where Next?

Popular in Questions Top

earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
RisingFromAshes
I've read in another post that it may be possible with a router helper - but I couldn't find an appropriate one, and tbh, I'm still just ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement