ken-kost

ken-kost

Live image preview storing

I have a modal with form and live image upload which uploads selected image on form submit. Also on the form a button for submodal can appear which opens another modal on top of form modal and after closing it you get back to the form. All the changes will remain, except for the image preview which is not in changeset but in uploads.

What I want is the selected image preview remains after submodal close. One of my attempts was to store the entry in changeset i put it back on load/change, which worked but it did not solve my issue.

So I dug deeper and as far as I understand, when I call live_img_preview which calls content_tag with data_phx_entry_ref which is used deep in the javascript live_uploader.js

static getEntryDataURL(inputEl, ref, callback){
    let file = this.activeFiles(inputEl).find(file => this.genFileRef(file) === ref)
    callback(URL.createObjectURL(file))
  }

And then I get the error

Uncaught TypeError: URL.createObjectURL: Argument 1 is not valid for any of the 1-argument overloads.
    getEntryDataURL live_uploader.js:28
    mounted hooks.js:36
    __mounted view_hook.js:17
    performPatch view.js:341
    trackAfter dom_patch.js:59
    trackAfter dom_patch.js:59
    perform dom_patch.js:191
    perform dom_patch.js:191
    performPatch view.js:362
    update view.js:451
    time live_socket.js:208
    update view.js:448
    applyPendingUpdates view.js:503
    applyPendingUpdates view.js:503
    pushLinkPatch view.js:964
    pushWithReply view.js:636
    matchReceive push.js:76
    matchReceive push.js:76
    startTimeout push.js:107
    trigger channel.js:278
    Channel channel.js:70
    trigger channel.js:278
    onConnMessage socket.js:510
    decode serializer.js:25
    onConnMessage socket.js:497
    onmessage socket.js:221
    connect socket.js:221
    doConnect live_socket.js:191
    connect live_socket.js:195
    <anonymous> app.js:46
    <anonymous> app.js:4396
live_uploader.js:28:17
    getEntryDataURL live_uploader.js:28
    mounted hooks.js:36
    __mounted view_hook.js:17
    performPatch view.js:341
    trackAfter dom_patch.js:59
    forEach self-hosted:164
    trackAfter dom_patch.js:59
    perform dom_patch.js:191
    forEach self-hosted:164
    perform dom_patch.js:191
    performPatch view.js:362
    update view.js:451
    time live_socket.js:208
    update view.js:448
    applyPendingUpdates view.js:503
    forEach self-hosted:164
    applyPendingUpdates view.js:503
    pushLinkPatch view.js:964
    pushWithReply view.js:636
    matchReceive push.js:76
    forEach self-hosted:164
    matchReceive push.js:76
    startTimeout push.js:107
    trigger channel.js:278
    Channel channel.js:70
    trigger channel.js:278
    onConnMessage socket.js:510
    decode serializer.js:25
    decode self-hosted:1151
    onConnMessage socket.js:497
    onmessage socket.js:221
    (Async: EventHandlerNonNull)
    connect socket.js:221
    doConnect live_socket.js:191
    connect live_socket.js:195
    <anonymous> app.js:46
    <anonymous> app.js:4396

I googled the error, one response is
https://chromestatus.com/feature/5618491470118912
that createObjectUrl is deprecated.

window.URL.createObjectURL(file) Replace with window.URL.srcObject = file

But I’m not sure if that’s the issue, or that the file disappears after changing modal/route.

phoenix liveview

Most Liked

LostKobrakai

LostKobrakai

For uploads LV maintains a lot more state than you get to interact with (including tmp files and such). Therefore you cannot just set values back like with a changeset. If those entries are to be maintained you want to go for a strategy where LV never get’s rid of those entries in the first place. That might be possible by patching between modals and keeping the upload allowed on both, but I’m not sure about that.

i-n-g-m-a-r

i-n-g-m-a-r

This works for me.

def upload_names, do: [:name_a, :name_b, :etc]

def cleanup_previews(socket) do
  Enum.reduce upload_names(), socket, fn n, s ->
    {_completed, in_progress} = uploaded_entries(s, n)

    Enum.reduce in_progress, s, fn e, s ->
      cancel_upload(s, n, e.ref)
    end
  end
end

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;...
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I fore...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35953 110
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement