简体   繁体   中英

How to Upload file in javaScript?

When we are uploading a file in html basically we write

<input type="file" />

and this code give ChooseFile button, no file chosen

But what I want is to create a Hyperlink and on clicking the hyperlink for it to work like an input of type="file" type - eg:

<a href="#" >ChooseFile</a>

any suggestions?

Don't think that you can style an input=file button beyond a certain limit,

check these links

http://www.quirksmode.org/dom/inputfile.html

Style input type file?

Or you can try a ready made plugin to do the job for you

https://github.com/blueimp/jQuery-File-Upload

i think this is help to you..

<!DOCTYPE html>
    <html>
    <body>

    <p>Click on one of the text labels to toggle the related control:</p>

    <div>
      <a href='#UPLOAD' >Upload a file</a>  
      <input type='file'  name='file1'  
           style='cursor:pointer; opacity:0.01; alpha(opacity=1); position:relative; z-index:200; left: -10em; margin:-0.3em;' />   
    </div>

    </body>
    </html>

尝试使用现有的jquery插件上传文件,我建议使用以下插件: http : //blueimp.github.io/jQuery-File-Upload/

Your best bet is to use a well-documented trick that will ultimately involve hiding the file input element as a child of a div that is styled to meet your needs. I say this is well documented as it is a very FAQ here on SO, and is covered on a number of other sites and blog posts on the internet.

Do not use javascript to click your file input element. You will likely run into security restrictions put in place by browsers. Some browsers may not even allow this. Others such as IE, will throw a security error when/if you attempt to send the file via javascript after using javascript to click the file input element.

If you want to avoid re-inventing the wheel, you can use a pre-built library that handles the complexity of cross-browser file uploading for you. I recommend Fine Uploader . It is well supported, quite active, and evolving quickly. Full Disclosure: I am the primary maintainer of this library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM