js implementation input type='file' file upload sample code
Dec 24, 2016 pm 05:14 PMIn development, file uploading is essential. <input type="file" /> is a commonly used upload tag, but it is ugly and cannot be changed when browsing. We usually use, <input type="file" />Hide, click on other tags (pictures, etc.) to realize the file upload function.
Look at the code:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="js/jquery/jquery-1.8.2.min.js" type="text/javascript"></script> <style type="text/css"> ._box { width: 119px; height: 37px; background-color: #53AD3F; background-image: url(images/bg.png); background-repeat: no-repeat; background-position: 0 0; background-attachment: scroll; line-height: 37px; text-align: center; color: white; cursor: pointer; } .none { width: 0px; height: 0px; display: none; } </style> <title>js 实现 input file 文件上传 /></title> </head> <body> <form id="form1" runat="server" method="post" enctype="multipart/form-data"> <div> <div class="_box">选择图片</div> </div> <div class="none"> <input type="file" name="_f" id="_f" /> </div> </form> </body> </html> <script type="text/javascript"> jQuery(function () { $("._box").click(function () { $("#_f").click(); }); }); </script>
However, in Firefox and some high-version browsers, the files to be uploaded can be obtained in the background. Some low-version browsers cannot obtain Request.Files at all.
Looking up the information, some said it should be changed to this :
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src="js/jquery/jquery-1.8.2.min.js" type="text/javascript"></script> <style type="text/css"> ._box { width: 119px; height: 37px; background-color: #53AD3F; background-image: url(images/bg.png); background-repeat: no-repeat; background-position: 0 0; background-attachment: scroll; line-height: 37px; text-align: center; color: white; cursor: pointer; } .none { width: 0px; height: 0px; display: none; } </style> <title>js 实现 input file 文件上传 /></title> </head> <body> <form id="form1" runat="server" method="post" enctype="multipart/form-data"> <div> <div class="_box">选择图片</div> </div> <div class="none"> <input type="file" name="_f" id="_f" /> </div> </form> </body> </html> <script type="text/javascript"> jQuery(function () { $("._box").click(function () { return $("#_f").click(); }); }); </script>
Added a return keyword, the compatibility has been improved a lot, but some browsers are still not easy to use.
We found that we can definitely get the file to be uploaded by manually clicking <input type="file" /> in the background
So we can transparently <input type="file" />
Modify the code as follows:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> ._box { position: relative; width: 119px; height: 37px; background-color: #53AD3F; background-image: url(images/bg.png); background-repeat: no-repeat; background-position: 0 0; background-attachment: scroll; line-height: 37px; text-align: center; color: white; cursor: pointer; overflow: hidden; z-index: 1; } ._box input { position: absolute; width: 119px; height: 40px; line-height: 40px; font-size: 23px; opacity: 0; filter: "alpha(opacity=0)"; filter: alpha(opacity=0); -moz-opacity: 0; left: -5px; top: -2px; cursor: pointer; z-index: 2; } </style> <title>js 实现 input file 文件上传 /></title> </head> <body> <form id="form1" runat="server" method="post" enctype="multipart/form-data"> <div> <div class="_box"> <input type="file" name="_f" id="_f" /> 选择图片 </div> </div> </form> </body> </html>
When we click to select the picture, we actually click on the <input type="file" /> with an opacity of 0. A single user cannot see the <input type="file" /> and can also get the information to be uploaded in the background. file.
ok
Summary:
Use an <input type="file" /> with an opacity of 0 to cover the label (or image, etc.) that you want the user to see, so that the user can click on it.
Use width height line-height font-size to control the size of the browse button on the right of <input type="file" />.
Use left top (right, bottum) to control the position of the browsing button on the right side of <input type="file" />, which can be set to a negative value.
Use z-index to set their layer coverage relationship.
form must have enctype="multipart/form-data" tag to upload files
For more js implementation input type="file" file upload sample code related articles, please pay attention to PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Replace String Characters in JavaScript

Custom Google Search API Setup Tutorial

8 Stunning jQuery Page Layout Plugins

Improve Your jQuery Knowledge with the Source Viewer

10 Mobile Cheat Sheets for Mobile Development
