floodico

floodico

Attach files to `live_file_input` from js

Hello community,

I’ve been exploring the possibility of programmatically setting files in the browser and associating them with a live_file_input in a Phoenix LiveView application. I’ve attempted the following script, but it doesn’t seem to be working as expected:

let file = new File([e.detail.blob], "avatar_preview.png", { type: "image/png" });
let fileInput = document.querySelector('input[name="avatar_preview"]');
let dataTransfer = new DataTransfer();
dataTransfer.items.add(file);
fileInput.files = dataTransfer.files;

Unfortunately, the file does not appear to be assigned to the live_file_input as intended. I’ve tried various approaches, such as updating the files property multiple times, but without success.

Is there a way to programmatically update the live_file_input or its associated ref data attributes to achieve the desired file association? Any insights or guidance on how to approach this challenge would be greatly appreciated.

Thank you!

First 2 of 2 Posts Switch mode

floodico

floodico OP

found the solution

import LiveUploader from "phoenix_live_view/assets/js/phoenix_live_view/live_uploader"

///...

const file = new File([blob], "avatar_preview.jpeg", { type: "image/jpeg" });
LiveUploader.trackFiles(fileInput, files)
fileInput.dispatchEvent(new Event("input", {bubbles: true}))
DaAnalyst

DaAnalyst

I didn’t find this necessary because in my case the vanilla approach with DataTransfer works fine (the live_file_input must be empty at the time you set its files though).

However, what I’m still missing is how to make it track progress when doing it this way. At first it’s stuck at 0% and next thing you know it uploaded. I imagine this is because I am bypassing its default mechanism, but it would help to know what else needs to be done so that it all works as if triggered by user.

UPDATE: False alarm. It works properly with progress and all. It was just that the file was too small. With bigger files the progress is shown.

— All posts loaded —

Where Next?

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement