asp.net下文件上傳和文件刪除的代碼
更新時間:2007年09月10日 21:45:21 作者:
文件上傳
HttpPostedFile postFile = Request.Files["imgFile"];
if(postFile.FileName!=String.Empty){
ex=postFile.FileName.Substring(postFile.FileName.LastIndexOf("."));
fileName= DateTime.Now.ToString("yyyyMMdd") + ex;
absPath=System.Web.HttpContext.Current.Server.MapPath("/img_ad/"+adTypeEn+"/";);
if(!System.IO.Directory.Exists(absPath)) System.IO.Directory.CreateDirectory(absPath); //上傳文件,要檢查一下是否建立了相關(guān)目錄
postFile.SaveAs(absPath+fileName);
}
文件刪除
string sqlFile="select ('/img_ad/'+Ltrim(Rtrim(compName))+'/'+Ltrim(Rtrim(fileName)))as adFile from adinfo where id="+sid;
object objAd=SqlComd.CreateSqlScalar(sqlFile);
if(objAd!=null){
string fName = Server.MapPath(objAd.ToString());
if(File.Exists(fName)) File.Delete(fName); //刪除文件,別忘了檢查一下有沒有這個文件
}
HttpPostedFile postFile = Request.Files["imgFile"];
if(postFile.FileName!=String.Empty){
ex=postFile.FileName.Substring(postFile.FileName.LastIndexOf("."));
fileName= DateTime.Now.ToString("yyyyMMdd") + ex;
absPath=System.Web.HttpContext.Current.Server.MapPath("/img_ad/"+adTypeEn+"/";);
if(!System.IO.Directory.Exists(absPath)) System.IO.Directory.CreateDirectory(absPath); //上傳文件,要檢查一下是否建立了相關(guān)目錄
postFile.SaveAs(absPath+fileName);
}
文件刪除
string sqlFile="select ('/img_ad/'+Ltrim(Rtrim(compName))+'/'+Ltrim(Rtrim(fileName)))as adFile from adinfo where id="+sid;
object objAd=SqlComd.CreateSqlScalar(sqlFile);
if(objAd!=null){
string fName = Server.MapPath(objAd.ToString());
if(File.Exists(fName)) File.Delete(fName); //刪除文件,別忘了檢查一下有沒有這個文件
}
相關(guān)文章
.NET性能優(yōu)化ValueStringBuilder拼接字符串使用實例
這篇文章主要為大家介紹了.NET性能優(yōu)化ValueStringBuilder拼接字符串的使用實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
asp.net下將純真IP數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫中的代碼
純真IP數(shù)據(jù)庫包含最新的IP信息,通過IP我們可以查詢訪問者的來路,地理位置!但下載下來的IP數(shù)據(jù)無法被我們直接調(diào)用,所以我們需要編寫代碼將IP寫入到數(shù)據(jù)庫中供我們使用!2011-01-01
Asp.Net使用服務(wù)器控件Image/ImageButton顯示本地圖片的方法
Image/ImageButton服務(wù)器控件顯示本地的圖片,實現(xiàn)思路是數(shù)據(jù)庫中存放了圖片的相對地址,讀取數(shù)據(jù)庫中的地址,用控件加載顯示圖片。具體實現(xiàn)步驟大家參考下本文2017-08-08

