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";
        }
    }
}

然后是要在 iframe 中显示的 preview.aspx 文件的页面加载事件:

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