protected void shangchuan_click(object sender, EventArgs e)
{
if (FileUpLoad1.HasFile)
{
try
{
//上传文件并指定上传目录的路径
String path=Server.MapPath("~/Files/")+ FileUpLoad1.FileName;
FileUpLoad1.PostedFile.SaveAs(path);
/*注意->这里为什么不是:FileUpLoad1.PostedFile.FileName
* 而是:FileUpLoad1.FileName?
* 前者是获得客户端完整限定(客户端完整路径)名称
* 后者FileUpLoad1.FileName只获得文件名.
*/ //当然上传语句也可以这样写(貌似废话):
//FileUpLoad1.SaveAs(@"D:\"+FileUpLoad1.FileName); lblMessage.Text = "上传成功!";
down.Visible = true;
downpath.Text = "http://172.18.67.102/Files/"+FileUpLoad1.FileName;
downpath.NavigateUrl = "http://172.18.67.102/Files/" + FileUpLoad1.FileName;
}
catch (Exception ex)
{
lblMessage.Text = "出现异常,无法上传!";
//lblMessage.Text += ex.Message;
}
}
else
{
lblMessage.Text = "尚未选择文件!";
}
}
//上传图片设置
<asp:TextBox ID="txtSubject" runat="server" Width="225px" Font-Size="9pt"></asp:TextBox>//主题图片名称
<asp:FileUpload ID="fileUp" runat="server" Font-Size="9pt" Width="231px" />//上传图片控件
<asp:Button ID="btnOK" CssClass="ButtonCss" runat="server" Font-Size="9pt" Text="确定"
OnClick="btnOK_Click" Height="21px" Width="63px" />//上传图片事件按钮
//cs页面上传图片
protected void btnOK_Click(object sender, EventArgs e)
{
string ID;
SqlConnection mycon = new SqlConnection(ConfigurationManager.AppSettings["conStr"]);
mycon.Open();
DataSet mydataset = new DataSet();
SqlDataAdapter mydataadapter = new SqlDataAdapter("select * from tb_Blog where UserName='" + Session["UserName"] + "'", mycon);
mydataadapter.Fill(mydataset, "tb_Blog");
DataRowView rowview = mydataset.Tables["tb_Blog"].DefaultView[0];
ID = rowview["BlogID"].ToString(); string P_str_filePath = "", P_str_fileExtName = "", P_str_mFileName, P_str_mPath;
System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传文件信息;<hr color=red>");
if ("" != fileUp.PostedFile.FileName)
{
P_str_filePath = fileUp.PostedFile.FileName;//获取文件路径
P_str_fileExtName = P_str_filePath.Substring(P_str_filePath.LastIndexOf(".") + 1);//获得扩展名
try
{
string personPhoto = Session["UserName"].ToString();
P_str_mPath = Server.MapPath("Images/Pictures/"+personPhoto+"/");//保存到指定的路径
P_str_mFileName =P_str_filePath.Substring(P_str_filePath.LastIndexOf("\\") + 1);//获得文件的名称
fileUp.PostedFile.SaveAs(P_str_mPath + P_str_mFileName);//保存到指定的路径下
string P_str_Com = "insert into tb_Picture(Subject,ImageUrl,BlogID) values('" + this.txtSubject.Text + "','Images/Pictures/" + personPhoto + "/" + P_str_mFileName + "','" + ID + "')";
SqlData da = new SqlData();
bool add = da.ExceSQL(P_str_Com);
if (add == true)
{
Response.Write("<script language=javascript>alert('添加成功!');location='PhotoManage.aspx'</script>");
}//codego.net/tags/11/1/
else
{
Response.Write("<script language=javascript>alert('添加失败!');location='javascript:history.go(-1)'</script>");
}
}
catch (Exception err)
{
Response.Write(err.ToString());
}
}
}