Javascript 选择文件后开始上传,使用 jQuery。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6774143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:07:58  来源:igfitidea点击:

Start upload after choosing a file, using jQuery.

javascriptjquery

提问by Mehd S

After having chosen a file for upload, I want the file to be uploaded to database without the click of a button. How is this done using jQuery?

选择要上传的文件后,我希望无需单击按钮即可将文件上传到数据库。这是如何使用 jQuery 完成的?

I would like to choose a file like this: http://i.stack.imgur.com/0408T.gif

我想选择这样的文件:http: //i.stack.imgur.com/0408T.gif

<input type="file" valign="baseline" />

回答by binarious

Assuming you're using a form:

假设您正在使用表单:

// select the file input (using a id would be faster)
$('input[type=file]').change(function() { 
    // select the form and submit
    $('form').submit(); 
});

EDIT:To keep this answer up-to-date:

编辑:要使此答案保持最新:

There is a nice way to upload files via AJAX without hacks described here: http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/

有一种通过 AJAX 上传文件的好方法,无需此处描述的 hack:http: //net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/

回答by genesis

Use jQuery's plugin uploadify

使用jQuery的插件uploadify

It's great plugin which has many options, and auto-upload, too

这是一个很棒的插件,有很多选项,还有自动上传

$(document).ready(function() {
  $('#file_upload').uploadify({
    'uploader'  : '/uploadify/uploadify.swf',
    'script'    : '/uploadify/uploadify.php',
    'cancelImg' : '/uploadify/cancel.png',
    'folder'    : '/uploads',
    'auto'      : true
  });
});

回答by fbasti2003

just set the auto to true.. 'auto':true

只需将自动设置为 true .. 'auto':true