0

奇妙な理由で、ファイルをアップロードするときに次のコードは何も出力しません。HTML5 / jQuery は何かを返すと思っていましたが、変数を出力しても何も返されません。

<? print_r($_FILES) ?>
<? print_r($_POST) ?>
<? print_r($_GET) ?>
<? print_r($_REQUEST) ?>
<form enctype="multipart/form-data" action="/upload.php" 
method="post" class="putImages">
   <input name="media[]" type="file" multiple/>
   <input class="button" type="submit" alt="Upload" value="Upload" />
</form>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function() {
var data = new FormData($('input[name^="media"]'));     
jQuery.each($('input[name^="media"]')[0].files, function(i, file) {
    data.append(i, file);
});

$(':file').on('change', function() {
$.ajax({
    type: 'POST',
    data: data,
    url: 'http://localhost/upload.php',
    cache: false,
    contentType: false,
    processData: false,
    success: function(data){
        alert(data);
    }
});
});
});
</script> 
4

2 に答える 2

0

あまりにも多くのコードの代わりに、Uplodify ファイル アップロード プラグインを使用することをお勧めします。これはすばやく簡単です。

于 2013-02-15T04:22:47.753 に答える
0

jquery formドキュメントhttp://malsup.com/jquery/form/を使用できます

次のような簡単なデモ コード:

$(document).ready(function() { 
    var options = { 
        target:        '#output2',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 

        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 

        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 

    // bind to the form's submit event 
    $('#myForm2').submit(function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(options); 

        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    }); 
}); 

ここに例がありますhttp://malsup.com/jquery/form/file/

于 2013-02-15T04:29:41.330 に答える