LaraBABA's avatar

Unable to get a selected image src to get copied to a file input refs

I have been on this for a full day and I just cannot find an answer to it.

Imagine a form with an upload file input field. Next to it there is also a button called "select template". The user can either upload his own image or select an image from a given template.

Here is the problem, I am unable to send the selected image to the input field(so I can process it in my axios post later on).

This is the table where all the template files are showing up:

<table class="table table-borderless table-hover">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr v-for="data in imageGallery">
<td class="align-middle">{{ data.id }}</td>
<td class="align-middle">{{ data.title }}</td>
<td><img :src="'/storage/images/' + data.image" class="img-thumbnail" alt="" style="width: 75px;" @click="selectImage(data.id, $event)">
</td>
</tr>
</tbody>
</table>

When the user clicks on the above image, I would like the file to be set to this input field(which is the same input where the user can upload his own file).

<label class="btn btn-info btn-block btn-sm">
<i class="fas fa-upload text-white"></i>
<input
type="file"
id="file"
@change="onFileAttached(1)"
ref="file"
accept="image/png, image/jpeg"
hidden/>
</label>

I tried this:

selectImage(id, event){

this.$refs.files[0].src = event.files[0].src;
//or
this.$refs.files[0].src = event.target.src;
}

But no luck.....I am not understanding what I am missing :-(

Is there a way to manually take a URL from a given image to set this image to an input field?

Thanks in advance for guiding me a bit.

0 likes
4 replies
drewdan's avatar
drewdan
Best Answer
Level 15

As far as I am aware, there is no way to programmatically set the value of an input box, though in this case, I am not sure why you'd need too. I think this is security related, to prevent a website from reaching into your file system.

If you already have the "templates" on your server, which I assume you do because you are rendering them in the gallery in this Vue component, you just need to pass it the location of the file, and then do something with it server side.

There is nothing wrong with accepting two inputs, one for the actual file upload and one for a chosen template, and then passing them both back to the server with some validation rules to say it's one or the other.

Hope that helps!

1 like
LaraBABA's avatar

@drewdan Thanks Drewdan, Now I understand why I was struggling so much with this....I thought about using axios to pass the location of the file to it.

Snapey's avatar

@User476820 Please mark @drewdan answer as correct. It does not solve your issue but tells you why what you are trying to do cannot work and suggests an alternate approach

1 like
LaraBABA's avatar

@Snapey My apology, I totally forgot to mark his answer as correct. I tell you, this issue was such headache.... I tried everything I could....:-)

Please or to participate in this conversation.