0

首先我想问你不建议我求助于一个 jQuery 插件来解决我的问题。我只是不愿意让我的应用程序与插件一起工作(它阻止我学习!)

我有一个包含一堆字段的表单,我通过使用 jQuery 传递给我的后端$.post()这就是我的 jQuery 函数:

 $.post(
    "/item/edit",
    $("#form").serialize(),
    function(responseJSON) {
        console.log(responseJSON);
    },
    "html"
);

这就是我打开表单的方式:

<form action="http://localhost/item/edit" method="post" accept-charset="utf-8" class="form-horizontal" enctype="multipart/form-data"> 

这是由 codeigniter 的form_open()方法自动生成的(因此 action="" 有一个值。虽然这无关紧要,因为我在表单末尾没有提交按钮)

在我的#form我有这个作为我的文件输入:<input type="file" name="pImage" />

当按下适当的按钮并$.post()调用该方法时,我的后端只需像这样打印变量:print_r($_POST)并且在打印的变量中缺少“pImage”元素。我认为文件可能不会作为数组中的元素出现,所以我继续尝试使用此 codeigniter 函数上传文件:$this->upload->do_upload('pImage');我收到错误消息:“您没有选择要上传的文件。”

关于如何克服这个问题的任何想法?

4

3 回答 3

1

您不能使用 AJAX 发布图像,我也必须在这里找到PHP jQuery .ajax() 文件上传服务器端理解

最好的办法是使用隐藏的 iframe 模拟 ajax 调用,表单必须将 enctype 设置为 multipart/formdata

于 2012-11-13T23:47:30.930 回答
0

文件不会使用 AJAX 发送到服务器端PHP LETTER中最好和最简单的 JQuery Ajax 上传器之一

您只需要在标题中正常包含 js,Jquery 代码将如下所示

$.ajaxFileUpload({

    url:'http://localhost/speedncruise/index.php/snc/upload/do_upload',

    secureuri:false,
    fileElementId:'file_upload',
    dataType: 'json',
    data    : {
        'user_email' : $('#email').val()
    },
    success: function (data, status) {
      //  alert(status);
       // $('#avatar_img').attr('src','data');
       
    }
    ,
    error: function (data, status, e) {
        console.log(e);
    }
});

希望这可以帮助你

于 2012-11-14T10:52:16.853 回答
0

我不能用 codeigniter 和 Ajax 做到这一点,我将图像传递给 base64 并在控制器中再次转换为文件

//the input file type
<input id="imagen" name="imagen" class="tooltip" type="file" value="<?php if(isset($imagen)) echo $imagen; ?>">

//the js
$(document).on('change', '#imagen', function(event) {
  readImage(this);
});

function readImage(input) {
    var resultado='';
    if ( input.files && input.files[0] ) {
        var FR= new FileReader();
        FR.onload = function(e) {
            //console.log(e.target.result);
            subirImagen(e.target.result);
        };       
        FR.readAsDataURL( input.files[0] );
    }
}

function subirImagen(base64){
    console.log('inicia subir imagen');
    $.ajax({
        url: 'controller/sube_imagen',
        type: 'POST',
        data: {
            imagen: base64,
        }
    })
    .done(function(d) {
        console.log(d);
    })
    .fail(function(f) {
        console.log(f);
    })
    .always(function(a) {
        console.log("complete");
    });
}

//and the part of de controller
public function sube_imagen(){
    $imagen=$this->input->post('imagen');
    list($extension,$imagen)=explode(';',$imagen);
    list(,$extension)=explode('/', $extension);
    list(,$imagen)=explode(',', $imagen);
    $imagen = base64_decode($imagen);
    $archivo='archivo.'.$extension;
    file_put_contents('imagenes/'.$archivo, $imagen);
    chmod('imagenes/'.$archivo, 0777); //I use Linux and the permissions are another theme
    echo $archivo; //or you can make another thing
}

ps.:对不起我的英语 n_nU

于 2015-03-10T15:59:55.710 回答