【问题标题】:Simple jQuery AJAX file upload简单的 jQuery AJAX 文件上传
【发布时间】:2014-12-24 16:23:29
【问题描述】:

我完全是 jQuery 的菜鸟,并且迫切希望让它工作。

这是html:

<form method="post" enctype="multipart/form-data">
    <input id="pic "type="file" name="file" onchange="javascript:this.form.submit();">
</form>

jQuery:

$("#pic").change(function() {
    var file_data = $('#pic').prop('files')[0];
    var form_data = new FormData();
    form_data.append('file', file_data)
    alert(form_data);
    $.ajax({
                url: 'doupload.php',
                dataType: 'text',
                cache: false,
                contentType: false,
                processData: false,
                data: form_data,
                type: 'post',
                success: function(dat){
                    alert('it works maybe');
                }
     });
});

所以我只想将文件发送到 doupload.php 并用 ($_FILES['file']['tmp_name']) 在那里捕获它

但它不工作(ofc),我没有找到任何在谷歌或堆栈上工作的东西......

我使用这个图书馆:&lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"&gt;&lt;/script&gt;

【问题讨论】:

  • 你遇到了什么错误?
  • 如何查看错误?
  • 查看 javascript 控制台并确保在 php 中设置了 error_reporting(E_ALL)
  • 你的意思是把error_reporting(E_ALL)放到doupload.php中?它甚至没有连接到它:/
  • 您可以尝试使用最新版本的 javascript 并使用 $.post() 而不是 $.ajax() 吗?

标签: php jquery file-upload


【解决方案1】:
<input id="pic "type="file" name="file" onchange="javascript:this.form.submit();">

你有 "type="file"

把它改成 type="file"

此外,如果您通过 "$("#pic").change(function() { 发送更改时的 ajax,那么您也不应该使用 onchange="javascript:this.form.submit();",因为它会在 ajax 仍在发送时提交表单,从而导致可能的时间问题(例如 ajax通话未完成)

据我所知,您根本不应该有提交事件,因为数据已经通过 ajax 调用提交。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-17
    • 1970-01-01
    • 2017-11-08
    • 2021-12-15
    相关资源
    最近更新 更多