derek-zhou
How do I modify the upload content before the upload?
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?
Marked As Solved
derek-zhou
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
Also Liked
mcrumm
One option is to use a hidden <.live_file_input /> and then use this.upload() on a client-side hook to enqueue the resized file. Here is a brief example:
In your HEEx template:
<!-- Users select files with this file input. -->
<input type="file" id="resize-target" phx-hook="ResizeInput" data-upload-target="images" />
<form phx-change="change" phx-submit="submit">
<!-- The live file input is hidden because users will not interact with it directly. -->
<.live_file_input upload={@uploads.images} style="display:none;" />
<button type="submit">Upload</button>
</form>
…and the ResizeInput hook:
// assets/js/resize_input.js
import { convertResizeImageFiles } from './image_utils'
// This hook attaches to a custom input element to resize selected images.
export default ResizeInput = {
getUploadTarget() { return this.el.dataset.uploadTarget },
mounted() {
this.el.addEventListener("change", (e) => {
e.preventDefault()
e.stopImmediatePropagation()
if (this.el.files && this.el.files.length > 0) {
convertResizeImageFiles(this.el.files, 300, (resizedImageBlob) => {
// Enqueues the resized blob on the <.live_file_input />.
this.upload(this.getUploadTarget(), [resizedImageBlob])
})
}
})
}
}
I put the visible file input outside of the live form so that its change events don’t trigger additional messages to the server.
You can find a working demo in my live_upload_example repo: Add client-side resize demo · mcrumm/live_upload_example@5c797aa · GitHub
Hope that helps!
PS– Please forgive my JavaScript. The convertResizeImageFiles callback should really receive a list of files, that way the upload function would only need to be invoked once for each batch of selected files, however I am currently on vacation so this is the best I could do on short notice ![]()
sreyansjain
This is very nice and can be used to consume files directly on the server instead of loading to an external storage.
Thank you so much for sharing (while being on vacation, really appreciate it.).
Enjoy your vacation.
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
Last Post!
sreyansjain
This is very nice and can be used to consume files directly on the server instead of loading to an external storage.
Thank you so much for sharing (while being on vacation, really appreciate it.).
Enjoy your vacation.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









