先贴出来 ajax代码
function outto()
{
$("#showxz").hide()
if ($("#ids").attr("value") == "") {
alert('未选中任何数据');
return;
}
var a = $("#ids").attr("value");
var t = a.split(',');
//都完成了才可以显示下载文件名
if (confirm("您要操作的数据条数为 " + t.length + ",确认后继续?") == true) {
$("#ssj").val('......系统正在压缩中请稍后.....');
setTimeout(function () {
$.ajax({
type: "GET",
url: "/xxhz_admin/filemore/",
//temp:预防360缓存内容,保存iis的sessionID
data: { ids: a, temp: Math.random() },
async: true,//异步之后不影响本页其他功能
dataType: "json",
success: function (data) {
if (data.name == '')
{
$("#ssj").val('......压缩完成.....');
$("#showxz").show();
}
// alert(data.ret);
//alert(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//alert(XMLHttpRequest.status);
//alert(XMLHttpRequest.readyState);
//alert(textStatus);
}
});
}, 1000);
}
}
页面中
<input type="text" id="ssj" name="ssj" style="width:200px;" />
<div id="showxz" style="display:none; float:right; width:400px;">
<a href="/temprar/1.rar" target="_blank">【 下载文件1 】</a>
</div>
后台代码,包含,目录操作和文件的操作,还有压缩,注意WinRAR程序所在的位置
public JsonResult filemore(string ids)
{
string ttt_rpath = Server.MapPath("/temprar/yuan1");
// string s_rpath="D:\\Web\\weixin\\upload\\";
List<string> str = new List<string>();
string nstr = "";
string s_rpath = "E:\\CASE\\文化表彰投票\\修改\\weixin\\upload\\";
string errstr = "",ret="0";//空就是没有错误,
List<string> ids_array = ids.Split(',').ToList<string>();
var list = from a in base_db.SB_XMGL
where (ids_array).Contains(a.ID.ToString())
select a;
var res = new JsonResult();
//1查找文件是否在
foreach (var item in list)
{
nstr = s_rpath+"\\"+item.SB_PC_ID+"\\"+item.SB_order+"\\"+item.SB_NAME + ".doc";
str.Add(nstr);
if (!string.IsNullOrEmpty(item.IMG1))
{
string[] ab = item.IMG1.Split(',');
for (int i = 0; i < ab.Length; i++)
{
nstr = s_rpath + "\\" + item.SB_PC_ID + "\\" + item.SB_order + "\\Protocol\\" + ab[i];
str.Add(nstr);
}
}
if (!string.IsNullOrEmpty(item.IMG2))
{
string[] ab = item.IMG2.Split(',');
for (int i = 0; i < ab.Length; i++)
{
nstr = s_rpath + "\\" + item.SB_PC_ID + "\\" + item.SB_order + "\\Implementation\\" + ab[i];
str.Add(nstr);
}
}
if (!string.IsNullOrEmpty(item.IMG3))
{
string[] ab = item.IMG3.Split(',');
for (int i = 0; i < ab.Length; i++)
{
nstr = s_rpath + "\\" + item.SB_PC_ID + "\\" + item.SB_order + "\\Outcome\\" + ab[i];
str.Add(nstr);
}
}
if (!string.IsNullOrEmpty(item.IMG4))
{
string[] ab = item.IMG4.Split(',');
for (int i = 0; i < ab.Length; i++)
{
nstr = s_rpath + "\\" + item.SB_PC_ID + "\\" + item.SB_order + "\\Supplement\\" + ab[i];
str.Add(nstr);
}
}
}
foreach (string item in str)
{
if(!System.IO.File.Exists(item))
{
errstr = "丢失文件:" + item;
}
}
if (errstr == "")
{
string savePath = ttt_rpath+"";//获得当前的文件目录
//删目录下所有文件
DirectoryInfo dir = new DirectoryInfo(savePath);
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
foreach (FileSystemInfo i in fileinfo)
{
if (i is DirectoryInfo) //判断是否文件夹
{
DirectoryInfo subdir = new DirectoryInfo(i.FullName);
subdir.Delete(true); //删除子目录和文件
}
else
{
System.IO.File.Delete(i.FullName); //删除指定文件
}
}
foreach (var item in list)
{
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
nstr = s_rpath + "\\" + item.SB_PC_ID + "\\" + item.SB_order + "\\" + item.SB_NAME + ".doc";
System.IO.File.Copy(nstr, savePath + "\\" + item.SB_NAME + ".doc", true);
if (!string.IsNullOrEmpty(item.IMG1))
{
string[] ab = item.IMG1.Split(',');
if (!Directory.Exists(savePath + "\\" + item.SB_order + "\\Protocol\\"))
{
Directory.CreateDirectory(savePath + "\\" + item.SB_order + "\\Protocol\\");
}
for (int i = 0; i < ab.Length; i++)
{
nstr = s_rpath + "\\" + item.SB_PC_ID + "\\" + item.SB_order + "\\Protocol\\" + ab[i];
System.IO.File.Copy(nstr, savePath + "\\" + item.SB_order + "\\Protocol\\" + ab[i], true);
}
}
if (!string.IsNullOrEmpty(item.IMG2))
{
string[] ab = item.IMG2.Split(',');
if (!Directory.Exists(savePath + "\\" + item.SB_order + "\\Implementation\\"))
{
Directory.CreateDirectory(savePath + "\\" + item.SB_order + "\\Implementation\\");
}
for (int i = 0; i < ab.Length; i++)
{
nstr = s_rpath + "\\" + item.SB_PC_ID + "\\" + item.SB_order + "\\Implementation\\" + ab[i];
System.IO.File.Copy(nstr, savePath + "\\" + item.SB_order + "\\Implementation\\" + ab[i], true);
}
}
if (!string.IsNullOrEmpty(item.IMG3))
{
string[] ab = item.IMG3.Split(',');
if (!Directory.Exists(savePath + "\\" + item.SB_order + "\\Outcome\\"))
{
Directory.CreateDirectory(savePath + "\\" + item.SB_order + "\\Outcome\\");
}
for (int i = 0; i < ab.Length; i++)
{
nstr = s_rpath + "\\" + item.SB_PC_ID + "\\" + item.SB_order + "\\Outcome\\" + ab[i];
System.IO.File.Copy(nstr, savePath + "\\" + item.SB_order + "\\Outcome\\" + ab[i], true);
}
}
if (!string.IsNullOrEmpty(item.IMG4))
{
string[] ab = item.IMG4.Split(',');
if (!Directory.Exists(savePath + "\\" + item.SB_order + "\\Supplement\\"))
{
Directory.CreateDirectory(savePath + "\\" + item.SB_order + "\\Supplement\\");
}
for (int i = 0; i < ab.Length; i++)
{
nstr = s_rpath + "\\" + item.SB_PC_ID + "\\" + item.SB_order + "\\Supplement\\" + ab[i];
System.IO.File.Copy(nstr, savePath + "\\" + item.SB_order + "\\Supplement\\" + ab[i], true);
}
}
}
}
//2复制文件
//3压缩文件 string ttt_rpath = Server.MapPath("/temprar/yuan1");
//开始压缩
if (errstr == "")
{
ProcessStartInfo startinfo = new ProcessStartInfo(); ;
Process process = new Process();
string rarName = "1.rar"; //压缩后文件名
string path = ttt_rpath; //待压缩打包文件夹
string rarPath = Server.MapPath("/temprar"); //压缩后存放文件夹
string rarexe = @"E:\CASE\文化表彰投票\修改\winrar360\WinRAR.exe"; //WinRAR安装位置
string cmd = string.Format("a {0} {1} -r",
rarName,
path);
startinfo.FileName = rarexe;
startinfo.Arguments = cmd; //设置命令参数
startinfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏 WinRAR 窗口
startinfo.WorkingDirectory = rarPath;
process.StartInfo = startinfo;
process.Start();
process.WaitForExit(); //无限期等待进程 winrar.exe 退出
if (process.HasExited)
{
ret = "1";//代表压缩成功
}
}
var datae =
//new filemore();
//var q = from a in ty
// select new
new
{
ret = ret,
name = errstr
};
res.Data = datae;
res.JsonRequestBehavior = JsonRequestBehavior.AllowGet;//允许使用GET方式获取,否则用GET获取是会报错。
return res;
}