jQuery AjaxFormプラグインを使用して 、ユーザーがサーバーにロードするために選択した画像のプレビューを作成しています。Httpハンドラーを使用して、画像プレビューに使用される一時画像ファイルを作成します。FileUploadコントロールのクライアント側の「onchange」イベントに応答して、画像プレビューが正常に作成されます。
フォーム全体(UploadImageToServerStep1.aspx)をサーバーに送信するボタンがあります。そのボタンのサーバー側のClickイベントで、コントロールを別のページ(UploadImageToServerStep1.aspx2)に転送しようとすると、コントロールはファイルの背後にある他のページコードに到達します(コントロールはそのページに到達しますPage_Loadイベント)が、ページは表示されません-代わりに、参照ページが再び表示されます(UploadImageToServerStep1.aspx)(コントロールはそのページのページ読み込みイベントに移動しません)。
UploadImageToServerStep1.aspxのJSコードは次のとおりです。<scripttype= "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();
}
}; </ script> /
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;
}
誰かがその行動の原因を書いてくれませんか、そしてどうすればこの問題を解決できますか
ありがとう