brainbolt
I’m pretty new to Phoenix, so bear with me here ![]()
I’m integrating the Trix editor (GitHub - basecamp/trix: A rich text editor for everyday writing · GitHub) into a LiveView. I’ve got the text-editing features working, but now I’m working on file uploads, and I’m wondering if it is possible to use live uploads to make this work.
File uploads in Trix are accomplished by listening for a javascript event emitted by the editor on the client:
Storing Attached Files
Trix automatically accepts files dragged or pasted into an editor and inserts them as attachments in the document. Each attachment is considered pending until you store it remotely and provide Trix with a permanent URL.
To store attachments, listen for the
trix-attachment-addevent. Upload the attached files with XMLHttpRequest yourself and set the attachment’s URL attribute upon completion. See the attachment example for detailed information.
Is it possible to initiate a live upload from client-side javascript like this? If so, can anyone point me in the right direction on how to get started?
Much appreciated ![]()
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
chrismccord
phoenix hooks expose a
this.upload/uploadTofunction to do exactly that JavaScript interoperability — Phoenix LiveView v1.2.5brainbolt
Oh bummer, I must have missed that when I looked through the docs. Thanks!
tomasz-tomczyk
Apologies for resurrecting an old thread, I’m just trying to use Trix + Phoenix LiveView myself. Did you get to a working solution with
upload()?I’ve got as far as this:
Custom
inputcomponent that has thephx-hook="Trix"and contains a hiddenlive_file_inputcomponent to capture filesIn JS, callback for uploading files (omitted some of Trix event listeners:
This correctly sends data to the component and I get it all here:
Ignoring the fact that
destisn’t an actual URL to the file, even if I generate one, I don’t think I can pass it back to the JS –attachment.setAttributes({ url: "https://via.placeholder.com/150" });expects a “saved” URL that’s permanent.I’m now leaning towards a standalone controller to handle uploads as described in here How to handle file upload using Trix editor in a Phoenix application but I wondered if I’m missing something.
f0rest8
What do you mean by “I don’t think I can pass it back to the JS”? You could have your
handle_progress/3return a:replyinstead of:noreplythat contains the url you want? You could alsopush_event/3and listen for that event on the client side — I’m not sure I understand what issue you’re having.I used another method to do similar to the example provided by the trix documentation and the link you included at the bottom of your post and use a controller that then implements the uploader and upload logic.
Similary, removing attachments in the trix-editor calls
delete/2:In the uploader you can implement processing the file as well as sending the request. In our case for Mosslet, we check the image for safety, presign urls, and encrypt the image before sending it to cloud storage.
This works well and then I implement more checks in the
trix.jsto communicate with the server to handleundo/redoof attachments and ensuring the form isn’t submitted before the attachments are uploaded to the cloud and urls are ready to be saved to the database.