Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get value of input type="file"?

I have a <input type="file" id="uploadPicture" value="123">

When I'm using: alert($("#uploadPicture").val());

It alerts an empty dialog.

like image 905
Danpe Avatar asked May 05 '11 19:05

Danpe


People also ask

How do you show the value of an input type file?

The only way to set the value of a file input is by the user to select a file. This is done for security reasons. Otherwise you would be able to create a JavaScript that automatically uploads a specific file from the client's computer.

How does HTML input type file work?

The input element, having the "file" value in its type attribute, represents a control to select a list of one or more files to be uploaded to the server. When the form is submitted, the selected files are uploaded to the server, along with their name and type.

How do you attach a file in HTML?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute.

How do I hide the selected text file in HTML?

You can give the input element a font opacity of 0. This will hide the text field without hiding the 'Choose Files' button.


1 Answers

@BozidarS: FileAPI is supported quite well nowadays and provides a number of useful options.

var file = document.forms['formName']['inputName'].files[0]; //file.name == "photo.png" //file.type == "image/png" //file.size == 300821 
like image 127
Dmytro Vyprichenko Avatar answered Sep 17 '22 21:09

Dmytro Vyprichenko