使用FormData ajax()的多个输入时,jQuery文件上传失败 [英] jQuery file upload fails using several inputs with FormData ajax()

查看:128
本文介绍了使用FormData ajax()的多个输入时,jQuery文件上传失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑 我试图通过带有文件的jQuery/ajax()发送多个输入值,而ajax()似乎无法同时支持两者.我发现FormData可以解决问题. 使用jQuery ajax上载文件和表单数据formData() http://www.thefourtheye.in/2013/10/file-upload-with-jquery-and-ajax.html

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. I've found that FormData would do the trick. Using jQuery ajax to upload file and form data with formData() http://www.thefourtheye.in/2013/10/file-upload-with-jquery-and-ajax.html

更新 我没有任何错误了,但是文件没有正确上传到MySQL DB/LongBlob列中.看来现在可以在PHP文件中正确接收它了.

UPDATE I don't have any errors anymore but the file is not uploaded properly into the MySQL DB/LongBlob columb. It seems to be properly received now in the PHP file.

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

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

仅供参考,我正在使用jQuery 1.9.1 欢迎评论/建议 问候

FYI, I'm using jQuery 1.9.1 comments/suggestions are welcome regards

推荐答案

好,我终于明白了.只是一个笨拙的凌乱,"遗留在php文件中.现在一切正常. 感谢您的帮助!

Ok I got it finally. Just a stupid messy "," left behind in the php file. Now all works properly. Thanks for help!

这篇关于使用FormData ajax()的多个输入时,jQuery文件上传失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆