<tr>
<td align="right" style="width: 101px; height: 31px;"> </td>
<td align="left" style="width: 661px; height: 31px">
<asp:TextBox ID="TxtPicPath" runat="server" Enabled="False"></asp:TextBox>
<asp:Button ID="BtnDelPic" runat="server" Text="删除首页图片" CausesValidation="False" OnClick="BtnDelPic_Click" Enabled="False" /></td>
</tr>
protected void BtnUpload_Click(object sender, EventArgs e)
{
//Response.Write("zz");
//Response.End();
string filepath = UpLoad(FileUp, "uploads/IndexPic/");
if (filepath.Length > 4)
{
LblUpResult.Text = "上传成功!";
TxtPicPath.Text = filepath;
this.BtnDelPic.Enabled = true;
}
}
if (TxtPicPath.Text != "")
{
LblUpResult.Text = "";
DelFile(TxtPicPath.Text);
TxtPicPath.Text = "";
}
using System.IO;
page_load()
{
if (!IsPostBack) { }
}
#region 删除单个文件
public static bool DelFile(string Path)
{
try
{
File.Delete(System.Web.HttpContext.Current.Server.MapPath(Path));
return true;
}
catch (Exception ex)
{
return false;
}
}
#endregion
#region 上传文件
public string UpLoad(System.Web.UI.WebControls.FileUpload F, string LoadPathe)
{
string FilePath = "";
if (F.HasFile)
{
string Filename = F.FileName;//文件名
string FileExen = Filename.Substring(Filename.LastIndexOf(".") + 1).ToLower();//扩展名
//string[] AllowExen = new string[] { "gif", "png", "jpg", "bmp", "rar", "zip", "doc" };
string[] AllowExen = new string[] { "gif", "png", "jpg", "bmp", "swf" };
int flag = 0;
for (int i = 0; i < AllowExen.Length; i++)
{
if (FileExen.Equals(AllowExen[i]))
{
flag++;
}
}
if (flag > 0)
{
string newFileName;
//上传路径后是否带有"/",没有就加上
string LoadPath = "./" + LoadPathe;
/*if (LoadPath == "")
{
LoadPath = "./";
}
if (!LoadPath.Substring(LoadPath.Length - 1).Equals("/"))
{
LoadPath += "/";
}*/
//生成上传全路径
newFileName = Server.MapPath(LoadPath) + Filename;
//Response.Write(newFileName);
//Response.End();
if (File.Exists(newFileName))
{
System.Random rd = new Random();
string r = rd.Next(10, 99).ToString();
newFileName = newFileName.Insert(newFileName.LastIndexOf("."), "_" + r);
}
//Response.Write("a");
F.SaveAs(newFileName);
FilePath = LoadPathe + newFileName.Replace(System.Web.HttpContext.Current.Server.MapPath(LoadPath), "");
//Response.Write("b");
}
}
return FilePath;
}
#endregion
//一下为数据库操作的流程方式(不是针对图片上传)
string sqlStr = "update Product set Product_Hits=Product_Hits+1 where (Product_ID=" + Request.QueryString["ProductID"] + ")";
DataAccess da = new DataAccess();
da.Open(DataFactory.DbType.DataBaseType.Sql);
da.ExcuteNonQuery(sqlStr);
da.Close();
SqlConnection sqlcn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString);
sqlcn.Open();
SqlDataAdapter sqlda = new SqlDataAdapter("select * from Product,ShopDetail where Product_ID=" + Request.QueryString["ProductID"].ToString(),sqlcn);
DataSet ds = new DataSet();
sqlda.Fill(ds,"tb");
lbtitle.Text = ds.Tables["tb"].Rows[0]["Product_Name"].ToString();
lbshopname.Text = ds.Tables["tb"].Rows[0]["Shop_ShopName"].ToString();
lbtime.Text = ds.Tables["tb"].Rows[0]["Product_Time"].ToString();
lbhits.Text = ds.Tables["tb"].Rows[0]["Product_Hits"].ToString();
lbcontent.Text = ds.Tables["tb"].Rows[0]["Product_Info"].ToString();
ImagePhoto.ImageUrl = ds.Tables["tb"].Rows[0]["Product_Photo"].ToString();
sqlcn.Close();