简体   繁体   English

jQuery文件上传使用FormData ajax()的多个输入失败

[英]jQuery file upload fails using several inputs with FormData ajax()

EDIT I'm trying to send several input values trough jQuery / ajax() with a file in it and ajax() doesn't seems to support both in the same time. 编辑我正在尝试通过jQuery / ajax()发送几个带有文件的输入值,而ajax()似乎不同时支持两者。 I've found that FormData would do the trick. 我发现FormData可以解决问题。 Using jQuery ajax to upload file and form data with formData() http://www.thefourtheye.in/2013/10/file-upload-with-jquery-and-ajax.html 使用jQuery ajax通过formData()上传文件和表单数据 http://www.thefourtheye.in/2013/10/file-upload-with-jquery-and-ajax.html

UPDATE I don't have any errors anymore but the file is not uploaded properly into the MySQL DB/LongBlob columb. 更新我没有任何错误了,但是文件没有正确上传到MySQL DB / LongBlob列中。 It seems to be properly received now in the PHP file. 现在似乎已在PHP文件中正确接收了它。

html html

    <input type=hidden name=catselid id=catselid value=".$id.">
    <input name=city id=city>
    <input name=country id=country>
    <input type=file name=picture id=picture >
    <input name=update value=update type=submit class=update id=update  />

javascript javascript

    $(".update").click(function(){

        $.ajax({
            url: 'catsel_change.php',
            type: 'POST',
            contentType:false,
            processData: false,
            data: function(){
                var data = new FormData();
                data.append('picture',$('#picture').get(0).files[0]);
                data.append('city' , $('#cityname').val());
                data.append('country', $('#country').val());
                data.append('id', $('#catselid').val());
                return data;
            }(),
                success: function(result) {
                alert(result);
                },
            error: function(xhr, result, errorThrown){
                alert('Request failed.');
            }
            });

    });

php 的PHP

if(is_uploaded_file($_FILES['picture']['tmp_name'])){
    $picture =addslashes (file_get_contents($_FILES['picture']['tmp_name']));
...
}

FYI, I'm using jQuery 1.9.1 comments/suggestions are welcome regards 仅供参考,我正在使用jQuery 1.9.1的注释/建议

Ok I got it finally. 好吧,我终于明白了。 Just a stupid messy "," left behind in the php file. 只是一个愚蠢的凌乱的“,”遗留在php文件中。 Now all works properly. 现在一切正常。 Thanks for help! 感谢帮助!

There is no way of sending file to the server through a true ajax call. 无法通过真正的ajax调用将文件发送到服务器。 i recommend you to put your form in a hidden iframe and submit it from the parent. 我建议您将表单放入隐藏的iframe中,然后从父级提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM