weixin_33691598 2015-11-25 21:06 采纳率: 0%
浏览 40

jQuery文件上传和Ajax

So, I have the following form and js/php:

php

 <form enctype="multipart/form-data">
    <input id="fileupload"  type="file" name="files[]" class="files " onChange="UploadImage" accept='image/*'/>
    <input type="button" class="submit_form" value="submit">    
</form>

<?php 
   add_action( 'wp_ajax_UploadImage', 'UploadImage' );
   function UploadImage()
   {
     $upload_dir = wp_upload_dir();
     $files = $_FILES['files']; 
   //Some function
   }
?>

JS

function UploadImage(e)
{
jQuery('#fileupload').fileupload({
    url: upload_image.ajax_url,
});

if(jQuery('#fileupload')) {
    var form = document.forms.namedItem("upload_video"); 
    var formdata = new FormData(form); 
    formdata.append('action', 'UploadImage');
    jQuery.ajax({           
        success : function(data){                   
                alert('sddsf');
            }
    })
}
};

As you can see from here, when an image is selected using Blueimp jQuery File upload (which the js is not properly written), I want the image file to be handled by the php function.

In other words, the js is not correctly written and I am not sure how to initiate the plugin then when the image is selected, it is processed by the php function via ajax (meaning, how do I parse the file info to php function via ajax?)

  • 写回答

1条回答 默认 最新

  • weixin_33724570 2015-11-26 13:59
    关注

    Don't use $.ajax directly. The plugin already does that behind the scenes.

    Here's a working example, based on your code, but adapted to run on JSFiddle:

    $(document).ready(function(){
        var url = '/echo/json/';
        var formdata = {json: JSON.stringify({field1: 'value1'})};
    
        jQuery('#fileupload').fileupload({
            url: url,
            formData : formdata,        
            dataType: 'json',
            type: "POST",
            contentType:false,
            processData:false,
            success : function(data){                   
                alert('success...');
                console.dir(data);
            }
        });       
    });
    

    Demo: http://jsfiddle.net/pottersky/8usb1sn3/3/

    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目