0

我需要能够上传图像,然后让我上传的图像替换当前的背景 div。这必须从 .ascx 文件中完成。这是我的 WebUserControl.ascx 中的所有当前相关代码:

<div class="box">
<img src="EnergyOilGas-ProductionEnhanced-Oil_Natural-Gas-Recovery_flow_diagram3.jpg" id="myimage" alt="image" height="800px" width="1700px" style="position: absolute;"/>

<div>
    <asp:FileUpload ID="FileUpload1" runat="server" />
</div>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Upload" />

还有我要更改的元素的 .css:

.box
{
    border: 1px solid blue;
    margin-bottom: 8px;
    width: 1700px;
    height: 800px;
    float: none;
    clear: both;
    background-image: url('EnergyOilGas-ProductionEnhanced-Oil_Natural-Gas-Recovery_flow_diagram3.jpg');
}

而我背后的代码(WebUserControl.ascx.cs):

protected void Button2_Click(object sender, EventArgs e)
{

    if (FileUpload1.PostedFile != null)
    {
        string fileExt =
           System.IO.Path.GetExtension(FileUpload1.FileName);

        if (fileExt == ".jpeg" || fileExt == ".jpg")
        {

            string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

            string conn4 = "Host=172.21.98.153; Database=UAT_smartcomm; UserName=iDacAdmin; Password=iDacAdmin2008; port=3306; allow zero datetime = yes";
            string upload = "INSERT INTO ScadaImages (FileName, fileExt) VALUES (" + FileName + ", " + fileExt + ")";

            DataBase.DataBaseIO.Save(conn4, upload);

            string defaultImg = "EnergyOilGas-ProductionEnhanced-Oil_Natural-Gas-Recovery_flow_diagram3.jpg";
            string uploadedImg = "" + FileName + "" + fileExt + "";


            Image.DisabledCssClass.Replace(defaultImg, uploadedImg);

        }

    }

}

这个想法是将用户上传的任何图像设置为新的背景图像,替换当前的图像。我知道背景图像元素存储在 .box 中,我不确定这是否还需要一个函数,除了方法背后的代码。我已经坚持了好几天了。如果有人可以帮助我,我将不胜感激。

4

0 回答 0