Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I upload a new file on click of image button

Tags:

I have got a task to upload new file from the click of image button. My code is

    <label>File</lable>
    <input type="image" src="http://upload.wikimedia.org/wikipedia/commons/c/ca/Button-Lightblue.svg" width="30px"/>

On the click of this button I want to upload a new file.How can I do this? You can check my code from Demo

like image 373
Nithin Viswanathan Avatar asked Apr 25 '13 08:04

Nithin Viswanathan


People also ask

How do I upload a file to the click of icons?

Just add an label tag and wrap input tag inside label and hide input and give it a id which will be used on label for attribute. You can remove the for="..." if the input element is inside the label.

How do I open file upload dialog box on image click?

And on the button's click event write the jQuery code like : $('#OpenImgUpload'). click(function(){ $('#imgupload'). trigger('click'); });

How do I create a Choose file button?

You can simply use ::-webkit-file-upload-button in css and style your Choose file button.


1 Answers

You could add a hidden file input field, like:

<input type="image" src="http://upload.wikimedia.org/wikipedia/commons/c/ca/Button-Lightblue.svg" width="30px"/>
<input type="file" id="my_file" style="display: none;" />

And do:

$("input[type='image']").click(function() {
    $("input[id='my_file']").click();
});

Demo:: Updated Fiddle

like image 115
Sudhir Bastakoti Avatar answered Sep 18 '22 17:09

Sudhir Bastakoti