0

こんにちは、iframe を使用して、ボタンのクリック時に画像のプレビューを表示しています (他のすべての方法を試してもうまくいきませんでした) が、「「/Asset Manager」アプリケーションでサーバー エラーが発生しました。」 iframe のコード:

<iframe src="" id="iPreview" style="border: 0px #666 none;" scrolling="no"
 frameborder="1" marginheight="0px" marginwidth="0px" height="150px" width="180px"
 runat="server">
</iframe>

これは、画像を取得し、パスをセッションに保存し、ボタンのクリック時に iframe の URL を設定するコードです。

protected void btnPreview_1_Click(object sender, EventArgs e)
{
    if (!imgUpload_1.HasFile)
    {
        return;
    }

    string strFileName = Path.GetFileNameWithoutExtension(imgUpload_1.FileName.ToString());
    string ext = Path.GetExtension(imgUpload_1.FileName.ToString());
    string loc = "temp/";
    string strImgFolder = Server.MapPath(loc);

    System.Drawing.Image newImage;

    FileUpload img = (FileUpload)imgUpload_1;
    Byte[] imgByte = null;

    try
    {
        if (img.HasFile && img.PostedFile != null)
        {

            HttpPostedFile File = imgUpload_1.PostedFile;

            imgByte = new Byte[File.ContentLength];

            File.InputStream.Read(imgByte, 0, File.ContentLength);
        }
    }
    catch (Exception ex)
    {

    }

    if (imgByte != null)
    {
        using (MemoryStream stream = new MemoryStream(imgByte, 0, imgByte.Length))
        {
            newImage = System.Drawing.Image.FromStream(stream);

            newImage.Save(strImgFolder + strFileName + ext);

            Session["ImagePreview"] = "~/temp/" + strFileName + ext;
            this.iPreview.Attributes["src"] = "~/preview.aspx";
        }
    }
}

次に、preview.aspx ファイルのページ読み込みイベントを iframe 内に表示します。

protected void Page_Load(object sender, EventArgs e)
{
    if (!String.IsNullOrEmpty(Session["ImagePreview"] as string))
    {
        // Retrieving image path from session into local variable
        string imagePath = Session["ImagePreview"].ToString();
        // Assigning image url to variable containing the image path
        imgPreview.ImageUrl = imagePath;
    }
}

これは、preview.aspx ページのコードです。

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="preview.aspx.cs" Inherits="preview" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" style="width: 180px; height: 150px">
<head runat="server">
    <title></title>
</head>
<body style="width: 180px; height: 150px">
    <form id="form1" runat="server" style="width: 180px; height: 150px">
    <div style="width: 180px; height: 150px">
        <asp:Image ID="imgPreview" runat="server" Height="150px" Width="180px" />
    </div>
    </form>
</body>
</html>
4

0 に答える 0