簡體   English   中英

在ASP.NET中使用jQuery AjaxForm問題

[英]using jQuery AjaxForm in ASP.NET problem

我正在使用jQuery AjaxForm插件來創建用戶選擇加載到服務器的圖像的預覽。 我使用Http處理程序創建用於圖像預覽的臨時圖像文件。 響應FileUpload控件的客戶端“ onchange”事件,成功創建了圖像預覽。

我有一個按鈕,可以將整個表單(UploadImageToServerStep1.aspx)發送到服務器。 在該按鈕的服務器端Click事件中,我嘗試將控件轉移到另一個頁面(UploadImageToServerStep1.aspx2),該控件獲取到文件后面的另一個頁面代碼(該控件獲取到該頁面的Page_Load事件), 但該頁面未顯示-而是再次顯示引用頁面(UploadImageToServerStep1.aspx) (控件不會進入該頁面的頁面加載事件)。

UploadImageToServerStep1.aspx中的JS代碼為:<腳本類型=“ 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;

    }

有人可以寫出造成這種現象的原因是什么,如何解決這個問題?

謝謝

當嘗試對ajax調用進行response.redirect時,您的瀏覽器的行為將與常規同步請求不同。 但是,您可以在JS端獲取響應並對其進行處理。 我相信這會對您有所幫助。

返回重定向作為對XHR請求的響應

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM