

//导出事件
//先根据查询条件在服务器上的sp文件夹下找到符合条件的图片,把找到的图片放在~/sp/目录下的一个临时文件夹中,
//再把该文件夹压缩到~/sp/目录下生成压缩文件,最后再下载该压缩文件
protected void btnImageExport_Click(object sender, EventArgs e)
{
//获取sp文件夹路径
string Webpath = HttpContext.Current.Request.MapPath(pictureUrl);
string SourceFile = Server.MapPath(pictureUrl + stuRAR);
//事先要判断文件夹“学生照片”存不存在,如果不存在,则创建“学生照片”文件夹
if (!Directory.Exists(SourceFile))
{
Directory.CreateDirectory(SourceFile);
}
//删除文件夹中临时暂存的图片
foreach (string file in Directory.GetFileSystemEntries(SourceFile))
{
if (File.Exists(file))
File.Delete(file); //直接删除其中的文件
}
//查询符合条件的照片
strWhere = GetQeuryCondition();
DataTable dt = stuBll.GetPartStudentByWhere(strWhere);
//ArrayList arrNoFile = new ArrayList();
if (dt.Rows.Count != 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
string FileName = dt.Rows[i]["xh"].ToString();
CopyFileByFilePath(FileName, pictureUrl);
//if (NoFile != null)
//{
// arrNoFile.Add(NoFile);//把不存在图片的学生学号存放在arrNoFile中
//}
}
bool flag = RAR(pictureUrl + stuRAR, stuRAR + ".rar", pictureUrl);
if (flag == false)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "return1", string.Format("<script language=javascript>alert(\"" + "导出失败!" + "\");</script>"));
}
DownloadFileByFilePath(pictureUrl + stuRAR + ".rar");
}
else
{
this.ClientScript.RegisterStartupScript(this.GetType(), "return1", string.Format("<script language=javascript>alert(\"" + "不存在该班号!" + "\");</script>"));
}
}
/// <summary>
/// 拷贝文件
/// </summary>
/// <param name="FileName">要拷贝的文件名</param>
/// <param name="destFile">拷贝到的地址</param>
public void CopyFileByFilePath(string FileName, string destFile)
{
string FilePath = pictureUrl + FileName + ".jpg";//图片名字
destFile += stuRAR;
string destPath = Server.MapPath(destFile);//destPath为文件夹“学生照片”的路径
destFile += "\\" + FileName + ".jpg";//destFile为文件夹“学生照片”中图片文件的路径
string mpath = Server.MapPath(FilePath);//FilePath为文件夹sp中图片文件的路径,而“学生照片”是sp中的一个文件夹
destPath = Server.MapPath(destFile);
if (File.Exists(mpath))//判断以学号FileName命名的文件是否存在
{
File.Copy(mpath, destPath, true);// 把mpath路径下的文件拷贝到destPath路径下
//return null;
}
//else
//{ //没有图片则返回学号
// return FileName;
//}
}
/// <summary>
/// 压缩文件
/// </summary>
/// <param name="DFilePath">需要压缩的文件夹或者单个文件</param>
/// <param name="DRARName">生成的压缩文件的文件名</param>
/// <param name="DRARPath">生成的压缩文件的保存路径</param>
/// <returns></returns>
protected bool RAR(string DFilePath, string DRARName, string DRARPath)
{
//目标文件路径(生成的压缩文件的路径)
string DestFile = Server.MapPath(DRARPath + DRARName);
//源文件路径(需要压缩的文件的路径)
string SourceFile = Server.MapPath(DFilePath);
//把相对路径转化为绝对路径
DRARPath = Server.MapPath(DRARPath);
FileInfo ImageFileRAR = new FileInfo(DestFile);
FileInfo ImageFile = new FileInfo(SourceFile);
if (ImageFileRAR.Exists)
{
ImageFileRAR.Delete();
}
String the_rar;
RegistryKey the_Reg;
Object the_Obj;
String the_Info;
ProcessStartInfo the_StartInfo;
Process the_Process;
try
{
the_Reg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
the_Obj = the_Reg.GetValue("");
the_rar = the_Obj.ToString();
the_Reg.Close();
the_rar = the_rar.Substring(1, the_rar.Length - 7);
the_Info = " a " + " " + "" + " " + SourceFile; //命令 + 压缩后文件名 + 被压缩的文件或者路径
//the_Info = " a " + " " + DRARName + " " + SourceFile; //命令 + 压缩后文件名 + 被压缩的文件或者路径
the_StartInfo = new ProcessStartInfo();
the_StartInfo.FileName = the_rar;
the_StartInfo.Arguments = the_Info ;
the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
the_StartInfo.WorkingDirectory = DRARPath + stuRAR; //被压缩文件夹的路径(该文件夹中存放的是要压缩的图片)
the_Process = new Process();
the_Process.StartInfo = the_StartInfo;
the_Process.Start();
//下面的函数导致ListBox1不能显示绑定的数据
the_Process.WaitForExit();//让用户等待进程结束,我这里是要同时下载,否则可以不用写这句
the_Process.Close();
foreach (string d in Directory.GetFileSystemEntries(SourceFile))
{
if (File.Exists(d))
File.Delete(d); //直接删除其中的文件
}
return true;
}
catch (Exception ex)
{
return false;
}
}