我有一个用户控件,可以上传图像并将其显示给用户。在父页面上传图像后,我想将图像URL保存到数据库,但我不能。这是我使用的代码,但图像为空。
Image img = new Image();
img = this.Uploade1.FindControl("Image1") as Image;
<小时/> 我的代码:我的用户控件:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<contenttemplate>
<table class="auto-style1">
<tr>
<td class="auto-style2">
</td>
<td rowspan="3">
<asp:Image ID="Image1" runat="server" Height="100" ImageAlign="Middle" Width="100" />
<br />
<asp:Label ID="StatusLabel" runat="server" Font-Names="Tahoma" Font-Size="8pt" Visible="False"></asp:Label>
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:FileUpload ID="FileUploadControl" runat="server" />
</td>
</tr>
<tr>
<td class="auto-style2">
<asp:Button ID="btnUpload" runat="server" OnClick="ButtonUpload_Click" Text="بارگذاری" />
</td>
</tr>
</table>
<br />
</contenttempl
protected void ButtonUpload_Click(object sender, EventArgs e) { //Path to store uploaded files on server - make sure your paths are unique string Id = Request.QueryString["id"]; //string thumbPath = "../upFiles/_thumb_" + Guid.NewGuid().ToString() + ".jpg"; //string filePath = thumbPath.Replace("_thumb_", ""); string filePath = "../upFiles/" + Guid.NewGuid().ToString() + ".jpg"; try { // Check that there is a file if (FileUploadControl.HasFile) { if (FileUploadControl.PostedFile.ContentType == "image/jpeg") { if (FileUploadControl.PostedFile.ContentLength 0) && (System.IO.Path.GetExtension(myFile.FileName).ToLower() == ".jpg")) { // Read file into a data stream byte[] myData = new Byte[nFileLen]; myFile.InputStream.Read(myData, 0, nFileLen); myFile.InputStream.Dispose(); // Save the stream to disk as temporary file. // make sure the path is unique! System.IO.FileStream newFile = new System.IO.FileStream(Server.MapPath(filePath.Remove(0,3)), System.IO.FileMode.Create); newFile.Write(myData, 0, myData.Length); newFile.Close(); System.IO.File.Delete(Server.MapPath(filePath.Remove(0, 3) +"_temp.jpg")); //. new cpode Image1.ImageUrl = filePath.Remove(0, 3); } } } catch (Exception ex) { StatusLabel.ForeColor = System.Drawing.Color.Red; StatusLabel.Text = ex.Message; } }
答案 0 :(得分:1)
您可以使用会话保存图片网址,然后在子页面中检索并保存到数据库。
第一页:
Session["url"] = imageUrl;
第二页:
string url = (string)Session["url"] ;
//save to database