程序go 2015-02-21 21:11 采纳率: 50%
浏览 25

jQuery Ajax发布文件

I try to post an uploaded file via ajax to php.

HTML:

<form id="uploadForm" name="uploadForm" action="#" enctype="multipart/form-data"> 
<input id="name" name="name" class="form-control" type="text" />
<input id="csv" name="csv" type="file" />
<input id="CSVUpload" type="submit" class="btn btn-default" name="Submit" value="Hochladen" />

JS

$("#uploadForm").submit(function () {
    var formData = new FormData();
    formData.append( 'csv',  $( '#csv' )[0].files[0] ); 
    formData.append( 'name', $( '#name'));
    jQuery.ajax({
        url: "../lib/import.php",
        data: formData,
        type: "POST",
        success: alert('erfolgreich'),
        error: alert('Fehler'),
        cache: false,
        contentType: false,
        mimeType: 'multipart/form-data',
        processData: false
    });     
});

and then i try to get the form data in PHP:

$_FILES['csv']
$_POST['name']

But the Ajax call completes with success and error... I'm confused... The PHP file will not executed correctly...

Thanks for your help!

  • 写回答

2条回答 默认 最新

  • weixin_33736048 2015-02-21 22:30
    关注

    Try restructuring your AJAX call with your success and failure code like this:

    $.post('../lib/import.php', formData).done(function() {
        console.log('done');
    }).fail(function() {
        console.log('error');
    });
    

    See http://api.jquery.com/jquery.post/ for more examples.

    评论

报告相同问题?