上传文件时创建目录问题 为了实现将路径保存在数据库中,文件上传时保存在文件夹中,同时在指定的文件夹中创建不同的文件夹来保存不通类型的文件,必须在上传文件的同时,在指定目录下创建一目录,且要求,如果该目录已经存在,则直接利用;如果不存在,则需要新建。在删除数据库的记录的同时也能删除该文件夹,如果该文件夹中没有任何文件的话。试问该怎样实现? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /// <summary> /// 上传文件 /// </summary> /// <param name="fulFile">上传控件</param> /// <param name="DirectionName">文件所放的父文件夹</param> /// <returns></returns> public void FileUpLoad(FileUpload fulFile, string DirectionName) { string FileName; string FileSavePath; FileName = fulFile.FileName; FileSavePath = Server.MapPath("~/File/"); //获取文件存放的根路径 if (fulFile.HasFile) //判断是否选择了要上传的文件 { string name = fulFile.PostedFile.FileName; FileInfo file = new FileInfo(name); string newFileName = GetRandomString(10) + file.Extension; //随机设置上传后的文件名 //FileSavePath += DirectionName; //DirectoryInfo direction = new DirectoryInfo(FileSavePath); if (!File.Exists(FileSavePath + "\\" + DirectionName)) { Directory.CreateDirectory(FileSavePath + "\\" + DirectionName); //如果文件存放的文件夹不存在,则创建该文件夹 } FileSavePath += DirectionName + "\\" + newFileName; //string path = Server.MapPath(FileSavePath); if (!File.Exists(FileSavePath)) //判断所上传的文件文件是否存在 { try { fulFile.SaveAs(FileSavePath); } catch (Exception Error) { HttpContext.Current.Response.Write("<script>alert('" + Error.Message + "')</script>"); return; } } else { //在该处操作基本与上相同,只是不用创建文件夹了 } } else { HttpContext.Current.Response.Write("<script>alert('请选择要上传的文件')</script>"); return; } HttpContext.Current.Response.Write("<script>alert('文件上传成功')</script>"); } public void FileUpLoad(FileUpload fulFile, string DirectionName) { string FileName; string FileSavePath; FileName = fulFile.FileName; FileSavePath = Server.MapPath("~/File/"); //获取文件存放的根路径 if (fulFile.HasFile) //判断是否选择了要上传的文件 { string name = fulFile.PostedFile.FileName; FileInfo file = new FileInfo(name); string newFileName = GetRandomString(10) + file.Extension; //随机设置上传后的文件名 //FileSavePath += DirectionName; //DirectoryInfo direction = new DirectoryInfo(FileSavePath); if (!File.Exists(FileSavePath + "\\" + DirectionName)) { Directory.CreateDirectory(FileSavePath + "\\" + DirectionName); //如果文件存放的文件夹不存在,则创建该文件夹 } FileSavePath += DirectionName + "\\" + newFileName; //string path = Server.MapPath(FileSavePath); if (!File.Exists(FileSavePath)) //判断所上传的文件文件是否存在 { try { fulFile.SaveAs(FileSavePath); } catch (Exception Error) { HttpContext.Current.Response.Write(" <script>alert('" + Error.Message + "') </script>"); return; } } else { //在该处操作基本与上相同,只是不用创建文件夹了 } } else { HttpContext.Current.Response.Write(" <script>alert('请选择要上传的文件') </script>"); return; } HttpContext.Current.Response.Write(" <script>alert('文件上传成功') </script>"); } 大家说下是先conn.close()呢还是先comm.Dispose()呢? 急,求了手工向membership添加数据的例子,或者告诉我需要注意哪些地方? .net 权限问题 Session..Remove()为什么不起作用? 请问有开源的在线视频录制工具吗???FLASH的` 更换图片的技巧问题 C#为什么在子窗体中调用父窗没有作用 连接firebird问题 控件显灰 关于multipage和datagrid控件的问题求助!!!! AspNetPager 分页绑定-DataGrid汇总出错 新手诚信请教asp.net c# 操作xml的问题
/// 上传文件
/// </summary>
/// <param name="fulFile">上传控件</param>
/// <param name="DirectionName">文件所放的父文件夹</param>
/// <returns></returns>
public void FileUpLoad(FileUpload fulFile, string DirectionName)
{
string FileName;
string FileSavePath;
FileName = fulFile.FileName;
FileSavePath = Server.MapPath("~/File/"); //获取文件存放的根路径 if (fulFile.HasFile) //判断是否选择了要上传的文件
{
string name = fulFile.PostedFile.FileName;
FileInfo file = new FileInfo(name);
string newFileName = GetRandomString(10) + file.Extension; //随机设置上传后的文件名
//FileSavePath += DirectionName;
//DirectoryInfo direction = new DirectoryInfo(FileSavePath);
if (!File.Exists(FileSavePath + "\\" + DirectionName))
{
Directory.CreateDirectory(FileSavePath + "\\" + DirectionName); //如果文件存放的文件夹不存在,则创建该文件夹
}
FileSavePath += DirectionName + "\\" + newFileName; //string path = Server.MapPath(FileSavePath);
if (!File.Exists(FileSavePath)) //判断所上传的文件文件是否存在
{
try
{
fulFile.SaveAs(FileSavePath);
}
catch (Exception Error)
{
HttpContext.Current.Response.Write("<script>alert('" + Error.Message + "')</script>");
return;
}
}
else
{
//在该处操作基本与上相同,只是不用创建文件夹了 }
}
else
{
HttpContext.Current.Response.Write("<script>alert('请选择要上传的文件')</script>");
return;
}
HttpContext.Current.Response.Write("<script>alert('文件上传成功')</script>");
}
public void FileUpLoad(FileUpload fulFile, string DirectionName)
{
string FileName;
string FileSavePath;
FileName = fulFile.FileName;
FileSavePath = Server.MapPath("~/File/"); //获取文件存放的根路径 if (fulFile.HasFile) //判断是否选择了要上传的文件
{
string name = fulFile.PostedFile.FileName;
FileInfo file = new FileInfo(name);
string newFileName = GetRandomString(10) + file.Extension; //随机设置上传后的文件名
//FileSavePath += DirectionName;
//DirectoryInfo direction = new DirectoryInfo(FileSavePath);
if (!File.Exists(FileSavePath + "\\" + DirectionName))
{
Directory.CreateDirectory(FileSavePath + "\\" + DirectionName); //如果文件存放的文件夹不存在,则创建该文件夹
}
FileSavePath += DirectionName + "\\" + newFileName; //string path = Server.MapPath(FileSavePath);
if (!File.Exists(FileSavePath)) //判断所上传的文件文件是否存在
{
try
{
fulFile.SaveAs(FileSavePath);
}
catch (Exception Error)
{
HttpContext.Current.Response.Write(" <script>alert('" + Error.Message + "') </script>");
return;
}
}
else
{
//在该处操作基本与上相同,只是不用创建文件夹了 }
}
else
{
HttpContext.Current.Response.Write(" <script>alert('请选择要上传的文件') </script>");
return;
}
HttpContext.Current.Response.Write(" <script>alert('文件上传成功') </script>");
}