通过ajax上传包含文件的表单

时间:2013-11-22 04:01:34

标签: javascript jquery ajax

我正在尝试上传包含文字和文件的表单。 JS正在返回成功消息,但该文件未被上传,并且该条目未进入数据库。

形式:

<form class="post-form" action="javascript:void(0)" method="post" enctype="multipart/form-data">
    <input type="file" name="file" size="25" />
    <input name="title" type="text" maxlength="160" placeholder="Title..." required />
    <input type="submit" name="submit" value="Submit" />
</form>

JS:

$(document).on('submit', ".post-form", function(uploadVideo) {
uploadVideo.preventDefault();
var formData = new FormData((this)[0]);
$.ajax({
    type: 'post',
    url: 'upload.php',
    data: formData,
    success: function () {
        alert("that sh*t worked!");
    },
    error: function() {
        alert("didn't work");
        },
        cache: false,
        contentType: false,
        processData: false
    });
});

0 个答案:

没有答案