img的位置在哪里?

时间:2012-12-28 06:54:35

标签: asp.net-mvc asp.net-mvc-4

全部,假设您有View这样的代码。

<img src='@Url.Action("GetCaptchaImg")' alt='' />

当然,控制器中有一个名为Action的{​​{1}},它将GetCaptchaImg返回给View。

在FireFox中打开此视图后。我发现Html代码是

FileContentResult

<img alt="" src="/Controllername/GetCaptchaImg" /> 不是真正的物理路径。所以我的问题是这个src的真实物理路径是什么,如何通过Ajax调用img来改变图像?希望您能够帮助我 。感谢。

1 个答案:

答案 0 :(得分:1)

您可以对actionresult进行ajax调用,然后从中返回图像的名称,并在ajax调用的onccess上更改图像 或者你可以做这件事,我已经在我的项目中实现了 像这样制作HTML表格

@using (Html.BeginForm("ActionResult", "Controller", FormMethod.Post, new { @id = "ImgForm", @enctype = "multipart/form-data", name = "ImgForm", target = "UploadTarget" }))
{
}

将iframe设为表单的目标

 <iframe id="UploadTarget" name="UploadTarget" onload="UploadImage_Complete();" style="position: absolute;
        left: -999em; top: -999em;"></iframe>

您的上传控件                                                                        

然后上传图片并在表单上显示

 function UploadImage() {       
            $("#ImgForm").submit(); //form id
        }

        function UploadImage_Complete() {
            try {
                //Check to see if this is the first load of the iFrame
                if (isFirstLoad == true) {
                    isFirstLoad = false;
                    return;
                }

                //Reset the image form so the file won't get uploaded again
                document.getElementById("ImgForm").reset();
                //Grab the content of the textarea we named jsonResult .  This shold be loaded into 
                //the hidden iFrame.
                var newImg = $.parseJSON($("#UploadTarget").contents().find("#jsonResult")[0].innerHTML);

                if (newImg.IsValid) {
                    document.getElementById("dp").src = newImg.ImagePath;
                    document.getElementById('profile-pic').src = newImg.ThumbnailPath;
                    document.getElementById("change").style.display = "block";                          

                }

                // If there was an error, display it to the user
                if (newImg.IsValid == false) {
                    alert(newImg.Message);
                    return;
                }                      
            }
            catch (e) {
            }
        }

您的行动将如下所示

 public WrappedJsonResult ChangeImage(HttpPostedFileBase file)
{
}

并且您的WrappedJsonResult类看起来像

public class WrappedJsonResult : JsonResult
    {

        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.Write("<html><body><textarea id=\"jsonResult\" name=\"jsonResult\">");
            base.ExecuteResult(context);
            context.HttpContext.Response.Write("</textarea></body></html>");
            context.HttpContext.Response.ContentType = "text/html";
        }

    }