简体   繁体   English

如何使用jquery选择图像并上传?

[英]How to select Image using jquery and upload it?

I am trying to upload image using jQuery.ajax without a form. 我正在尝试使用jQuery.ajax上传图像而没有表单。

I know how to select values for input boxes 我知道如何为输入框选择值

var catSelect=$("#category").val();

But how do i do same for uploading a image (without making a form). 但是我如何为上传图像做同样的事情(没有制作表格)。

Html: HTML:

<div id="tnail"> 
   <input id="profile-image-upload" class="hidden" type="file"> 
   <img id="simg" height="126" width="224" src="'+tmb+'"> 
</div>

jquery: jQuery的:

$('#simg').on('click', function() { 
     $('#profile-image-upload').click();
});

 $('#profile-image-upload').change(function() {
     alert( "Handler for .change() called." );  
 });

You can use http://jsfiddle.net/Quwn6/ 你可以使用http://jsfiddle.net/Quwn6/

  $('#simg').on('click', function() { 
      $('#profile-image-upload').click();
  });

   $('#profile-image-upload').change(function() {
       var formData = new FormData(this.files[0]);
       $.ajax({
          url: '/echo/json/',
          type: 'POST',
          data: formData,
          //Options to tell JQuery not to process data or worry about content-type
          cache: false,
          contentType: false,
          processData: false
      });
  });

I typed jquery file upload site:github.com on Google and found blueimp's "jQuery File Upload" . 我在谷歌jquery file upload site:github.com输入了jquery file upload site:github.com ,发现了blueimp的“jQuery文件上传” It has been recently authored (2/26/2014) and has a lot of watchers. 它最近被创作(2014年2月26日)并且有很多观察者。 Hope that helps! 希望有所帮助!

In the FAQ for the plugin , it says: 插件的常见问题解答中 ,它说:

Does the plugin require a form or file input field? 插件是否需要表单或文件输入字段?

If you define the url (and probably paramName) Options, you can call the plugin on any element - no form or file input field required - and the drag&drop functionality will still work. 如果您定义了url(可能还有paramName)选项,则可以在任何元素上调用插件 - 无需任何表单或文件输入字段 - 并且拖放功能仍然有效。

To support browsers without XHR file upload capabilities, a file input field has to be part of the widget, or defined using the fileInput option. 要支持没有XHR文件上载功能的浏览器,文件输入字段必须是窗口小部件的一部分,或者使用fileInput选项定义。

You can also use site:stackoverflow.com to find relevant questions on here! 您也可以使用site:stackoverflow.com在这里找到相关问题!

Have a good one. 祝你有个好的一天。

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

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