When we do input text upload, HTML comes with the upload button is ugly, how to beautify it? Similarly: input checkbox landscaping, input Radio beautification is a truth.
The beautification idea of the input File Upload button is to set the previous button transparency opacity to 0, and then wrap the outer layer with a div to achieve the beautification function.
Note: When the type of input is file, its name must have a value, because this name will be the array name for uploading the file information.
<href= "javascript:;" class = "A-upload mr10" >< type= "file" name= "myFiles" ID= "" > Click here to upload the file </a>
. A-upload{padding:4px 10px;/*height:34px;*/Line-height:28px;position:relative;cursor:Pointer;Color:#fff;Background-color:#286090;Border-color:#204d74;Border-radius:4px;Overflow:Hidden;Display:Inline-block;*display:inline;*zoom:1; }. A-upload input{position:Absolute;font-size:100px; Right:0;Top:0;Opacity:0;Filter:Alpha (opacity=0);cursor:Pointer}. A-upload:hover{Color:#FFFFFF;background:#337ab7;Border-color:#204d74;text-decoration:None; }
The above landscaping, the default selection of files, the displayed file name is also hidden, then how to display the selected file name? It doesn't matter, we can use jquery to get the file name.
We can write a change event.
<ahref= "javascript:;"ID= "Upload"class= "A-upload mr10"><inputtype= "File"name=""ID="">Click here to upload the file</a>//Add a div that displays the file name<Divclass= "Showfilename"></Div>
Upload button
<button id= "uploadbtn" type= "button" class= "Btn btn-primary" > Uploads </button>
$ (function() {
Show hidden file name and upload status toggle
$ ('. Showfilename '). Hide ();
$ (' #uploadBtn '). Hide ();
$ ("#upload"). On ("Change", "input[type= ' file ']", function () {
var FilePath = $ (this). Val ();
If only upload picture if (filepath.indexof ("jpg")! =-1 | | filepath.indexof ("PNG")! =-1) {
if (FilePath) {
$ (". Fileerrortip"). HTML (""). Hide ();
var arr = filepath.split (' \ \ ');
var fileName = arr[arr.length-1];
$ ('. Showfilename '). Show ();
$ (' #uploadBtn '). Show ();
$ (". Showfilename"). HTML ("FileName selected:" + filename);
$ (' #upload '). Hide ();
} else {
$ (". Showfilename"). HTML ("");
$ (". Fileerrortip"). HTML ("You did not upload the file, or you uploaded a file of the wrong type!") "). Show ();
return False
}
});
});
When a file is not selected
After selecting a file
Upload file button Landscaping, upload files before and after state control