简体   繁体   English

在ASP.NET中使用jQuery AjaxForm问题

[英]using jQuery AjaxForm in ASP.NET problem

I am using the jQuery AjaxForm plugin to create an preview of an image I the user select to load to the server. 我正在使用jQuery AjaxForm插件来创建用户选择加载到服务器的图像的预览。 I use an Http Handler to create an temp image file that is used for the image preview. 我使用Http处理程序创建用于图像预览的临时图像文件。 In response to client side 'onchange' event of the FileUpload control the image preview is created successfully. 响应FileUpload控件的客户端“ onchange”事件,成功创建了图像预览。

I have a button that sends the whole form (UploadImageToServerStep1.aspx) to the server. 我有一个按钮,可以将整个表单(UploadImageToServerStep1.aspx)发送到服务器。 In the server side Click event of that button I try to transfer the control to another page (UploadImageToServerStep1.aspx2),the control gets to the other page code behind file(The control gets to that page Page_Load event) but the page is not displayed - instead the referring page is displayed(UploadImageToServerStep1.aspx)again (the control do not go the page load event of that page). 在该按钮的服务器端Click事件中,我尝试将控件转移到另一个页面(UploadImageToServerStep1.aspx2),该控件获取到文件后面的另一个页面代码(该控件获取到该页面的Page_Load事件), 但该页面未显示-而是再次显示引用页面(UploadImageToServerStep1.aspx) (控件不会进入该页面的页面加载事件)。

The JS code in UploadImageToServerStep1.aspx is : < script type = "text/javascript" > UploadImageToServerStep1.aspx中的JS代码为:<脚本类型=“ text / javascript”>

var preview = { ImagePreview: function(imageId) { 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();
}

}; }; < /script>/ </脚本> /

In UploadImageToServerStep1.aspx.cs 在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");
     }
}

In the HttpHandler : 在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;

    }

Can someone please write what is the cause for that behavior and how can I solve this problem 有人可以写出造成这种现象的原因是什么,如何解决这个问题?

Thanks 谢谢

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. 当尝试对ajax调用进行response.redirect时,您的浏览器的行为将与常规同步请求不同。 You can however, take the response and do something with it on the JS side. 但是,您可以在JS端获取响应并对其进行处理。 I believe this will help you. 我相信这会对您有所帮助。

Returning redirect as response to XHR request 返回重定向作为对XHR请求的响应

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

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