2

我正在使用jQuery AjaxForm 插件 来创建用户选择加载到服务器的图像的预览。我使用 Http 处理程序来创建用于图像预览的临时图像文件。响应 FileUpload 控件的客户端“onchange”事件,成功创建图像预览。

我有一个将整个表单(UploadImageToServerStep1.aspx)发送到服务器的按钮。在该按钮的服务器端 Click 事件中,我尝试将控件转移到另一个页面(UploadImageToServerStep1.aspx2),控件获取到文件后面的另一个页面代码(控件获取到该页面的 Page_Load 事件)但页面不显示- 再次显示引用页面(UploadImageToServerStep1.aspx)(控件不执行该页面的页面加载事件)。

UploadImageToServerStep1.aspx中的JS代码为:< script type = "text/javascript" >

var preview = { ImagePreview: function(imageId) {

    var formId = '<%= Form.ClientID %>';
    var fileUploadId = '<%= FileUpload1.UniqueID %>';
    var action = $('#' + formId).attr('action');
    var imageName = $("input[serverId = 'FileUpload1']").val();
    $('#' + formId).attr('action', './HttpHandlers/ImagesHandler.ashx?action=imagePreview&f=' + fileUploadId + '&i=' + imageName);
    $('#' + formId).ajaxForm(function() {
        $('#' + imageId).attr('src', './HttpHandlers/ImagesHandler.ashx?action=imagePreview&f=' + fileUploadId + '&i=' + imageName);
        $('#' + imageId).show();
        $('#' + formId).attr('action', action);
    });
    $('#' + formId).submit();
}

}; < /脚本>/

在 UploadImageToServerStep1.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

        FileUpload1.Attributes.Add("onchange", "preview.ImagePreview('htmlImgPreview');");
        FileUpload1.Attributes.Add("serverId", "FileUpload1");

    }
}


protected void btnNext_Click(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        Response.Redirect("UploadImageToServerStep2.aspx");
        //Server.Transfer("UploadImageToServerStep2.aspx");
     }
}

在 HttpHandler 中:

 case "imagePreview":
            string f = context.Request.QueryString.Get("f");
            string i = context.Request.QueryString.Get("i");

            const string uploadImageTempPath = "~/Images/TempImages/";
            if (!string.IsNullOrEmpty(context.Request.QueryString.Get("i")) && context.Request.Files[f] != null)
            {
                HttpPostedFile file = context.Request.Files[f];
                SaveImage(context, file, uploadImageTempPath, i);
            }
            context.Response.ContentType = GetContentType(context.Session["fileName"].ToString());

            if (context.Session["fileName"] == null || context.Request["i"] == null)
            {
                return;
            }

            byte[] byteArray1 =
                System.IO.File.ReadAllBytes(
                    context.Request.MapPath(string.Format("{0}{1}", uploadImageTempPath, context.Session["fileName"])));
            context.Response.BinaryWrite(byteArray1);
            break;

    }

有人可以写下这种行为的原因是什么,我该如何解决这个问题

谢谢

4

1 回答 1

0

When trying to do a response.redirect in response to an ajax call, your browser is not going to behave in the same way it would with a regular synchronous request. You can however, take the response and do something with it on the JS side. I believe this will help you.

Returning redirect as response to XHR request

于 2011-05-11T19:21:19.147 回答