文件上传
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);     //删除文件,别忘了检查一下有没有这个文件
 } 

以上就是【asp.net下文件上传和文件删除的代码】的全部内容了,欢迎留言评论进行交流!

赞(0) 踩(0)
发表我的评论

最新评论

  1. 暂无评论