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); //上传文件,要检查一下是否建立了相关目录
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); //上传文件,要检查一下是否建立了相关目录
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); //删除文件,别忘了检查一下有没有这个文件
}
相关文章
.NET性能优化ValueStringBuilder拼接字符串使用实例
这篇文章主要为大家介绍了.NET性能优化ValueStringBuilder拼接字符串的使用实例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2022-06-06
Asp.Net使用服务器控件Image/ImageButton显示本地图片的方法
Image/ImageButton服务器控件显示本地的图片,实现思路是数据库中存放了图片的相对地址,读取数据库中的地址,用控件加载显示图片。具体实现步骤大家参考下本文2017-08-08


最新评论