2

当我的应用程序启动时,此 JSonResult 不存在并且我想使用它时(我收到错误“TypeError”)

var newImg = $.parseJSON($("#UploadTarget").contents().find("#jsonResult")[0].innerHTML);

我想做什么?我想有一个事件告诉我这个对象存在或者这个对象改变了值。

 var newImg = $.parseJSON($("#UploadTarget").contents().find("#jsonResult")[0].innerHTML);

我该怎么写这个?

using System.Web.Mvc;

    namespace pol
    {
        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";
            }
        }
    }

当我单击按钮图像上传到服务器但结果从未打开重要方法 UploadImage_Complete();

@model po.Models.Stwna
@using (Html.BeginForm("UploadImage", "StronaGlowna", FormMethod.Post,
                                         new
                                             {
                                                 enctype = "multipart/form-data",
                                                 id = "ImgForm",
                                                 name = "ImgForm",
                                                 target = "UploadTarget"
                                             }))
{
    <input type="file" name="imageFile" />

    <input type="button" class="button" value="@Model.ZO" onclick="UploadImage()" />
}
<iframe id="UploadTarget" name="UploadTarget" onload="UploadImage_Complete();" style="position: absolute;
    left: -999em; top: -999em;"></iframe>
<div id="Images">
</div>
<script type="text/javascript">
    var isFirstLoad = true;

    function UploadImage() {
        $("#ImgForm").submit();
    }
    function UploadImage_Complete() {
        //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 there was an error, display it to the user
        if (newImg.IsValid == false) {
            alert(newImg.Message);
            return;
        }



        //Create a new image and insert it into the Images div.  Just to be fancy,
        //we're going to use a "FadeIn" effect from jQuery
        var imgDiv = document.getElementById("Images");
        var img = new Image();
        img.src = newImg.ImagePath;

        //Hide the image before adding to the DOM
        $(img).hide();
        imgDiv.appendChild(img);
        //Now fade the image in
        $(img).fadeIn(500, null);


       // $('#Images').text(newImg.ImagePath);
    }
</script>

MVC

[HttpPost]
        public WrappedJsonResult UploadImage(HttpPostedFileWrapper imageFile)
        {

            return new WrappedJsonResult
            {
                Data = new
                {
                    IsValid = true,
                    Message = string.Empty,
                    ImagePath = Url.Content(String.Format("http://a1.ec-images.myspacecdn.com/images01/29/4982945eb42646efafe2f94855ac3d21/l.jpg"))

                }
            };
        }

当我单击按钮两次时,我得到此图像,但是当我单击一次时,我没有得到任何结果。

4

1 回答 1

0

我决定使用 Timer 来制作这个Simple Timer

于 2013-04-28T08:29:06.883 回答