简体   繁体   English

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

[英]How to load images after image upload via ajax?

I've uploaded some photos via ajax. 我已经通过ajax上传了一些照片。 then the response is the json encoded image names. 那么响应就是json编码的图像名称。 I want to view images imminently after upload proccess.I've tried this: 我想在上传过程后立即查看图片。我已经尝试过:

JS:EDITED JS:已编辑

$('#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? 我怎样才能做到这一点?

trying to fix the code... 试图修复代码...

I assume ajaxForm is some sort of plugin, and xhr works as you think it does 我假设ajaxForm是某种插件,并且xhr可以像您认为的那样工作

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();
    }
}); 

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

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