derek-zhou

derek-zhou

Liveview’s upload is simple enough to use. However, I now have a need to modify the file content before the upload. Specifically, I want to scale down the image at the client side before sending the potentially very large file over the network. Is there any javascript hook that enable me to do that?

Showing Posts 1 to 10

sreyansjain

sreyansjain

Did you find a way to do it?

regex.sh

regex.sh

You can achieve that with custom client side JS, before submitting you have files available for manipulation.
Usefull links: LiveView Upload docs

sreyansjain

sreyansjain

Sorry I am not able to figure that out. How to do it? By using Hooks?
Where are the files stored? Would be very grateful if you can share some code sample.
Thanks.

regex.sh

regex.sh

I’m not sure what your use case is, but you can send events back and forward to preform some actions.
Here you can find example code for uploading files, and you can put some logic in validate to send client side some data or w/e you need and then handle push event on client and preform action on images or something else.

sreyansjain

sreyansjain

Thank You for the input.
I just want to downsize large images.
I can send events and recieve them on the client.
But I am not sure where the files are located that I need to modify.

I am thinking that for external uploaders live view provides a Uploader with entries. That might be a place to modify the files.

polypush135

polypush135

I was just sharing with someone about this yesterday.

In short, yeah everything is done client side via hooks.

In one example I load an image with a wasm lib via a canvas on the dom, that is used to resize.
The other, I upload file to a bucket, call third party scale api with bucket path, download scaled image back to clients re-upload with new signed url.

Edit: I should add its easy to overload the browser in the case of the canvas/wasm workflow.
On a phone loading several 24mb sized images in to canvas has been able to crash a mobile browser really fast and given the size of raw phone images now it was a common issue I seen. The better of the two approaches I found was to use a signed url to throw the image at a buck then trigger a worker to async resize the image and fetch it later and or move it from a temp bucket to a permanent place. Uploading large files to a bucket is a non issue and its was better to have a back up of the source upload in my case too. I also found minio for self hosting buckets which made going the object store approach a lot for effective for me. That said I would love it if there was a really good full client side solution to this. Y

derek-zhou

derek-zhou OP

Yes, I scale the image using client side js and store the result in a blob. Then I chunk the blob, basse64 encode them, and send the chunks in-band in with pushEvent

See here for the js part:

https://github.com/derek-zhou/gara/blob/main/lib/gara_web/components/header.hooks.js

mcrumm

mcrumm

Phoenix Core Team

To be clear, you have to do all these extra steps because you want to modify the file client-side and then upload it to your webserver and not to cloud storage, correct?

I wonder if LiveView should support a client-side callback for server uploads :thinking:

derek-zhou

derek-zhou OP

Yes. My way does not make the best use of the bandwidth, and it is streaming binary data from browser memory to server memory, so it won’t work for large files. On the other hand, it is fairly simple and there is no need to cleanup should the user navigate away.

The only thing I wish from Liveview is the support for binary messages without resorting to base64. I could use a raw socket but then I need to deal with coordination between 2 processes.

sreyansjain

sreyansjain

That would be very welcome. Especially for common use cases like resizing images before uploading (especially on mobile devices).

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
psy-q
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews