duande3134 2013-11-15 18:18
浏览 13
已采纳

如何通过ajax上传图片后加载图片?

I've uploaded some photos via ajax. then the response is the json encoded image names. I want to view images imminently after upload proccess.I've tried this:

JS:EDITED

$('#upload-form').ajaxForm({
        success: function(data) {
            $(".loader").hide();
            $("#status").html("");
            $.each(data, function(index, item) {
                $("#status").append("<img src='<?php echo base_url()."assets/img/"; ?>" + item.name +"' />");
            });
            return false;
        },
        complete: function(xhr) {
            $(".loader").hide();
            return false;
        },
        error : function(xhr) {
                $("#status").html(xhr.responseText);
                $(".loader").hide();
        }
    });

but still no luck. How can I do this?

  • 写回答

1条回答 默认 最新

  • douxuqiao6394 2013-11-15 18:24
    关注

    trying to fix the code...

    I assume ajaxForm is some sort of plugin, and xhr works as you think it does

    No need for two loops, and no need for an intermediate array, you can create image nodes directly.

    var baseUrl = "<?php echo base_url(); ?>" + "assets/img/"; 
    
    $('#upload-form').ajaxForm({
        success: function(xhr) {
            $(".loader").hide();
            $("#status").html("");
            var images = "";
            $.each(xhr, function(index, item) {
                images += "<img src='"+ baseUrl + item.name + "' />";
            });
            $("#status").append(images);
            return false;
        },
        complete: function(xhr) {
            $(".loader").hide();
            return false;
        },
        error : function(xhr) {
                $("#status").html(xhr.responseText);
                $(".loader").hide();
        }
    }); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办