【问题标题】:How to display the new image uploaded in cshtmlcshtml中上传的新图片如何显示
【发布时间】:2013-10-23 12:35:54
【问题描述】:

我有这样的图像控件:

<img src="@Url.Action("GetProfileImage", "Controller", new { id = Model.DocId})"

动作

public FileResult GetProfileImage(int id)
{
    var usr = new Whatever();
    byte[] barrImg = usr.GetProfilePhoto(id);
    return File(barrImg, "image/png");
}

我的保存方法是这样的:

function SavePhotoToDb() {
                var json;
                var data;
                $.ajax({
                    type: "POST",
                    url: "/Example/SaveProfilePhoto",
                    data: new FormData($("#form0").get(0)),
                    dataType: "html",
                    contentType: false,
                    processData: false,
                    success: saveItemCompleted(data),
                    error: saveItemFailed
                });
            }

        function saveItemCompleted(data) {
               //showing success notification;
               // what else I have to do here to display the new image
    }

当页面加载了已经上传的个人资料照片时,这可以正常工作。 但是,当上传新照片时,它应该显示新照片,但是当我上传新照片时它没有触发 GetProfileImage() 操作,即使我删除照片,它也会从 sql server 中删除,但页面仍然显示照片..

上传新照片怎么办,才能显示新照片,删除照片怎么办,不显示照片,因为我删除了照片。

【问题讨论】:

  • 尝试重新加载页面?
  • 我只是像这样显示成功通知: ShowSuccess("上传成功");我没有得到要编写的代码,以便在上传后显示新图像。

标签: asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

您可以将id 添加到img 标记

 <img id="myimage" src="@Url.Action("GetProfileImage", "Controller", new { id = Model.DocId})"

在JS函数中

 function saveItemCompleted(data) {
     //Force to reloaded image
     var image = new Image();
     image.src = $("#myimage").attr("src") + "?_=" + (new Date().getTime()) ;
     $("#myimage").attr("src", image.src) 
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-07
    • 2012-03-05
    • 1970-01-01
    • 2014-06-25
    相关资源
    最近更新 更多