1、根据条件来动态的创建附件上传的文件夹。
要保证目录具有写的权限。
//上传附件
if (this.fulLocation.HasFile)
{
try
{
string FileName = this.fulLocation.FileName;//获取文件名
string[] FileArray = FileName.Split('.');
string FileExt = FileArray[FileArray.Length - 1].ToString();//获取文件后缀
int FileSize = this.fulLocation.PostedFile.ContentLength;//获取文件大小
//生成随机数
Random oRandom = new Random();
string oStringRandom = oRandom.Next(9999).ToString();
//格式化日期作为文件名
long oStringTime = DateTime.Now.ToFileTimeUtc();
//组合成文件名
string PostFile = oStringTime + oStringRandom + "." + FileExt;
//定义部门文件夹(根据dropdownlist动态创建根文件夹)
string rootCategory = this.ddlD.SelectedValue;
//所要创建文件夹的名字(根据dropdownlist的选择项来动态的创建)
string CategoryDirectory = this.ddlCategory.SelectedItem.Text.Replace("-","");
string CategoryPath = Server.MapPath("~") + "//" + rootCategory + "//" + CategoryDirectory;
//如果文件夹不存在则创建
if (!Directory.Exists(CategoryPath))
{
Directory.CreateDirectory(CategoryPath);
}
this.fulLocation.SaveAs(CategoryPath + "//" + PostFile);
doc.Location = "~" + "//" + rootCategory + "//" + CategoryDirectory + "//" + PostFile;
doc.LocationName = FileName;
}
catch (Exception ex)
{
throw ex;
}
}
else
{
doc.Location = "";
}
2。如果执行删除操作,将同时加数据库中的数据和文件夹中的文件删除
//删除文档的同时删除附件
Document doc = ServiceLocator.DocumentService.LoadDocument(id);
string filePath = Server.MapPath(doc.Location);
FileInfo filedel = new FileInfo(filePath);
if (filedel.Exists)
{
filedel.Delete();
}
ServiceLocator.DocumentService.DeleteDocument(id);
JavaScript.Alert("恭喜您!操作成功!", this.Page);
DataBind_Document(1);
return;