Directory.CreateDirectory( url);//创建文件夹
Directory.Delete(url, true);//删除文件夹
Directory.Move(oldPath, newPath);//修改文件夹名称
System.IO.File.Exists(url)//删除文件
多文件上传(上传服务器文件夹及数据库)
i自连表保存文件信息
定义文件集合List<IFormFile> file
循环每一个文件
foreach (var file in file)
{
var filePath = $"{parentUrl}/{file.FileName}";
//判断该目录文件是否存在
var fileExists = dbcontext.shareFolders.Where(e => e.IsDelete != true).FirstOrDefault(e => e.FileUrl == filePath);
if (fileExists!=null)
{
return this.Ok(-2);
}
判断文件大小
//if (file.Length > 1024 * 1024 * 10) //10M TODO:(1mb=1024X1024b)
//{
// continue;
//}
//文件保存
using (var fs = System.IO.File.Create(webRootPath + filePath))
{
file.CopyTo(fs);
fs.Flush();
}
//获取文件名不包含后缀名
// var fileName = Path.GetFileNameWithoutExtension(filePath);
var fileName = Path.GetFileName(filePath);
//保存信息到数据库
var Folderlist = new ShareFolder()
{
TenantId=2,
FileName = fileName,
ParentFileId = parentList.FileId,
FileLevel = parentList.FileLevel + 1,
FileUrl = filePath,
CreatedTime = DateTime.Now,
CreatedBy = Convert.ToInt32(data.UserId)
};
dbcontext.shareFolders.Add(Folderlist);}
int r = await dbcontext.SaveChangesAsync();