在主页面中有如下的控件
<IFRAME name="upfile" align="default" src="Upload_Pic.aspx" frameBorder="0" width="100%" scrolling="no" height="25"></IFRAME>
<asp:textbox id="txtContent" runat="server" BackColor="WhiteSmoke" Rows="10" TextMode="MultiLine" Width="99%"></asp:textbox>Upload_Pic.aspx中:
<INPUT type="file" size="28" runat="server" id="myFile" name="myFile"><asp:button text="上传图片" name="Submit" ID="btnUpload" Runat="server"></asp:button>我想实现的是在Upload_Pic.aspx页面中把图片上传后,并在主页面的txtContent控件中出现[image src=刚刚上传的图片地址],请问改如何实现?
<IFRAME name="upfile" align="default" src="Upload_Pic.aspx" frameBorder="0" width="100%" scrolling="no" height="25"></IFRAME>
<asp:textbox id="txtContent" runat="server" BackColor="WhiteSmoke" Rows="10" TextMode="MultiLine" Width="99%"></asp:textbox>Upload_Pic.aspx中:
<INPUT type="file" size="28" runat="server" id="myFile" name="myFile"><asp:button text="上传图片" name="Submit" ID="btnUpload" Runat="server"></asp:button>我想实现的是在Upload_Pic.aspx页面中把图片上传后,并在主页面的txtContent控件中出现[image src=刚刚上传的图片地址],请问改如何实现?
Response.Write("<script>parent.document.all.txtContent.value='" + "你保存的路径" + "'</script>")
<tr bgColor="#c9e3ff">
<td vAlign="top"><FONT face="宋体"></FONT><asp:label id="Label1" runat="server" Width="85px" Font-Bold="True"> 产品图片:</asp:label></td>
<td><asp:textbox id="img" runat="server"></asp:textbox><FONT face="宋体"></FONT><BR>
<INPUT id="fileUP" type="file" name="File1" runat="server">
<asp:button id="btnimg" runat="server" Text="上传"></asp:button></td>
</tr>代码:using System.IO;
private void btnimg_Click(object sender, System.EventArgs e)
{
string filePath="",fileExtName="",mFileName,mPath;
if("" != fileUP.PostedFile.FileName)
{
DateTime da = new DateTime();
da = DateTime.Now;
filePath =fileUP.PostedFile.FileName;//取得文件路径
fileExtName= filePath.Substring(filePath.LastIndexOf(".")+1);
try
{
//取得与 Web 服务器上的指定虚拟路径相对应的物理文件路径。
mPath=Server.MapPath("../product/img/");
mFileName=filePath.Substring(filePath.LastIndexOf("\\")+1);;//取得文件名
if(fileExtName=="jpg"||fileExtName=="gif"||fileExtName=="bmp"||fileExtName=="JPG"||fileExtName=="GIF"||fileExtName=="BMP")
{
//保存上传文件到指定的目录
mFileName=da.ToString();
mFileName=mFileName.Replace(":","");
mFileName=mFileName.Replace(" ","");
mFileName=mFileName.Replace("-","");
mFileName=mFileName+"."+fileExtName;
fileUP.PostedFile.SaveAs(mPath + mFileName);
if(img.Text == "")
{
img.Text = "product/img/" + mFileName;
}
else
{
try
{
string path = Server.MapPath("../product/img/")+img.Text.Replace("product/img/","");
FileInfo fi2 = new FileInfo(path);
if(fi2.Exists)//如果存在
{
//删除新创建的文件.
fi2.Delete();
}
img.Text = "product/img/" + mFileName;
}
catch(Exception error)
{
Response.Write(error.ToString());
}
}
}
else
{
filePath="";
fileExtName="";
mFileName="";
mPath="";
Response.Write("<script>alert('您上传的文件格式不对!')</script>");
}
}
catch
{
Response.Write("<script>alert('上传错误!')</script>");
}
}
}
HttpPostedFile post = imgInput.PostedFile;
lent = post.ContentLength;
byte[] lentimg = new byte[lent];
Stream photostream = post.InputStream;
photostream.Read(lentimg, 0, lent);
sql = "insert into photoalbum(title,phosize,category,introduce,pho) values(@title,@size,@cate,@inte,@img)";
SqlCommand com = new SqlCommand(sql, con);
com.Parameters.Add("@title", SqlDbType.VarChar);
com.Parameters["@title"].Value = this.TextBox1.Text;
com.Parameters.Add("@size", SqlDbType.Int);
com.Parameters["@size"].Value = lent;
com.Parameters.Add("@cate", SqlDbType.VarChar);
com.Parameters["@cate"].Value = this.DropDownList1.Text;
com.Parameters.Add("@inte", SqlDbType.VarChar);
com.Parameters["@inte"].Value = this.TextBox2.Text;
com.Parameters.Add("@img", SqlDbType.Image);
com.Parameters["@img"].Value = lentimg;
con.Open();
com.ExecuteNonQuery();
con.Close();
Response.Write("添加成功");