简体   繁体   English

如何在javaScript中上传文件?

[英]How to Upload file in javaScript?

When we are uploading a file in html basically we write 当我们用html上传文件时,我们基本上写

<input type="file" />

and this code give ChooseFile button, no file chosen 并且此代码提供ChooseFile按钮, 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: 但是我想要的是创建一个超链接,然后单击超链接以使其像type="file"类型的输入一样工作-例如:

<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 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 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. 最好的选择是使用有据可查的技巧,该技巧最终会涉及将文件输入元素隐藏为div的子元素,该div的样式可以满足您的需求。 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. 我说这是有据可查的,因为在SO上这是一个非常常见的问题,并且在Internet上的许多其他站点和博客文章中也有涉及。

Do not use javascript to click your file input element. 不要使用javascript单击文件输入元素。 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. 如果您在使用javascript单击文件输入元素后尝试通过javascript发送文件,则IE等其他文件将引发安全错误。

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 . 我建议使用Fine Uploader It is well supported, quite active, and evolving quickly. 它得到了良好的支持,非常活跃,并且发展迅速。 Full Disclosure: I am the primary maintainer of this library. 全面披露:我是该库的主要维护者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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