Codeigniter multipart ajax表单提交不显示错误

时间:2015-11-18 09:37:56

标签: ajax codeigniter firefox file-upload

在我的Codeigniter 2.2项目中,我尝试使用ajaxcodeigniter inbuilt file upload上传图片。文件上传工作正常。但我的问题是,我只能在浏览器的响应字段中看到一些加密文本。

问题仅出在firefox浏览器上。它正在使用google chrome

这样我就无法调试错误。我根本找不到错误。我怎么能避免这个?任何帮助都可以得到赞赏

ajax电话

$('#myForm').submit(function(e) {
    e.preventDefault();
    var data = new FormData(this);
     $.ajax({
                url: page_url,
                type: 'POST',
                dataType: 'json',
                data: data,
                mimeType: "multipart/form-data",
                contentType: false,
                cache: false,
                processData: false,
                success: function(data) {
                    alert(data);
                },
                error: function() {
                    alert('error');
                }
       });
});

浏览器回复字段中的文字

PGRpdiBzdHlsZT0iYm9yZGVyOjFweCBzb2xpZCAjOTkwMDAwO3BhZGRpbmctbGVmdDoyMHB4O21hcmdpbjowIDAgMTBweCAwOyI+Cgo8aDQ+QSBQSFAgRXJyb3Igd2FzIGVuY291bnRlcmVkPC9oND4KCjxwPlNldmVyaXR5OiBOb3RpY2U8L3A+CjxwPk1lc3NhZ2U6ICBVbmRlZmluZWQgb2Zmc2V0OiAyPC9wPgo8cD5GaWxlbmFtZTogbWFzdGVyL21hc3RlcnNfY3RyLnBocDwvcD4KPHA+TGluZSBOdW1iZXI6IDY2MDwvcD4KCjwvZGl2PjxkaXYgc3R5bGU9ImJvcmRlcjoxcHggc29saWQgIzk5MDAwMDtwYWRkaW5nLWxlZnQ6MjBweDttYXJnaW46MCAwIDEwcHggMDsiPgoKPGg0PkEgUEhQIEVycm9yIHdhcyBlbmNvdW50ZXJlZDwvaDQ+Cgo8cD5TZXZlcml0eTogTm90aWNlPC9wPgo8cD5NZXNzYWdlOiAgVW5kZWZpbmVkIG9mZnNldDogMTwvcD4KPHA+RmlsZW5hbWU6IG1hc3Rlci9tYXN0ZXJzX2N0ci5waHA8L3A+CjxwPkxpbmUgTnVtYmVyOiA2NjI8L3A+Cgo8L2Rpdj57Im1zZyI6IiIsImVycm9yIjoiRmFpbGVkIHRvIEFkZCBQZW5zaW9uZXIgRGV0YWlscyIsImVycm9yX21lc3NhZ2UiOiIifQ==

1 个答案:

答案 0 :(得分:3)

您必须创建并将该文件推送到FormData中,如下所示:

$('#myForm').submit(function(e) {
    e.preventDefault();
    var data = new FormData(this);
    var fileObj = $('#imagefileUpload').get(0); // input type file element
    var img, reader, file;
    file = fileObj.files[0];
    /*match type of file*/
    if (!!file.type.match(/image.*/)) {
        if (window.FileReader) {
            reader = new FileReader();
            reader.onloadend = function(e) {
            };
            reader.readAsDataURL(file);
        }

        if (data) {
            data.append('imagefileupload', file);
        }
    }
    $.ajax({
        url: page_url,
        type: 'POST',
        dataType: 'json',
        data: data,
        mimeType: "multipart/form-data",
        contentType: false,
        cache: false,
        processData: false,
        success: function(data) {
            alert(data);
        },
        error: function() {
            alert('error');
        }
    });
});