gyhlw_dotnet/网站项目/VRS/Handler/Files.ashx.cs

799 lines
29 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
namespace VRS.Handler
{
/// <summary>
/// Files 的摘要说明
/// </summary>
public class Files : BaseHandler, IHttpHandler
{
DataService.BLL.pro_file bll_file = new DataService.BLL.pro_file();
DataService.BLL.pro_result bll_pro_result = new DataService.BLL.pro_result();
public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
baseContext = context;
context.Response.ContentType = "application/json";
CrossDomain();
if (null == context.Request["action"])
{
var result = GetResult(false, "缺少参数action");
context.Response.Write(result);
context.Response.End();
}
string action = context.Request["action"];
switch (action)
{
#region
case "addlianxi":
Add_Lianxi(context);
break;
case "dellianxi":
Delete_Lianxi(context);
break;
case "querylianxi":
Query_Lianxi(context);
break;
#endregion
//添加文件 参数 result_id考核结果id file :文件
case "Add":
Add(context);
break;
//添加cos文件
case "addcos":
AddCos(context);
break;
case "Update":
Update(context);
break;
//删除进度文件记录,同时删除文件 参数 file_id文件id
case "Delete":
Delete(context);
break;
// 按照文件 file_id 查询文件
case "queryByFileid":
QueryFiles(context);
break;
//按照进度ProgressId查询文件列表按日期分组 参数 ProgressId进度id
// case "queryByProgressId":
//queryByProgressId(context);
// break;
//按照考核结果 ResultId查询文件
case "queryByResultId":
queryByResultId(context);
break;
default:
var result = GetResult(false, "方法名不存在:" + action);
context.Response.Write(result);
break;
}
}
/// <summary>
/// 按照进度ProgressId查询所有文件列表按日期分组
/// </summary>
/// <param name="context"></param>
public void queryByResultId(HttpContext context)
{
var result_id = context.Request.Params["result_id"];
var sb = new StringBuilder();
sb.Append(" 1=1 ");
if (!string.IsNullOrWhiteSpace(result_id))
{
sb.Append(" and out_id='" + result_id + "' and r1='2' ");
}
var file = bll_file.GetModelList(sb.ToString()).FirstOrDefault();
var data = GetResult(file);
context.Response.Write(data);
context.Response.End();
}
/// <summary>
/// 按照进度ProgressId查询所有文件列表按日期分组
/// </summary>
/// <param name="context"></param>
public void queryByProgressId(HttpContext context)
{
var condtion = context.Request.Params["ProgressId"];
var sb = new StringBuilder();
sb.Append(" 1=1 ");
if (!string.IsNullOrWhiteSpace(condtion))
{
sb.Append(" and progress_id='" + condtion + "' and r1='2' ");
}
var listAll = bll_file.GetModelList(sb.ToString());
var result = new List<ProgressFileData>();
for (int i = 0; i < listAll.Count; i++)
{
var item = listAll[i];
var dateStr = item.create_time.Value.ToString("yyyy年MM月dd日");
var existsProgressFile = result.FirstOrDefault(s => s.DateDesc == dateStr);
if (null == existsProgressFile)
{
var progressFile = new ProgressFileData();
progressFile.DateDesc = dateStr;
progressFile.data = new List<DataService.Model.pro_file>();
progressFile.data.Add(item);
result.Add(progressFile);
}
else
{
existsProgressFile.data.Add(item);
}
}
var list_result = result.OrderByDescending(s => s.DateDesc).ToList();
var data = GetResult(list_result);
context.Response.Write(data);
context.Response.End();
}
///查询文件
public void QueryFiles(HttpContext context)
{
var data = context.Request.Params["file_id"];
var ret = string.Empty;
if (string.IsNullOrEmpty(data))
{
ret = GetResult(false, "file_id参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
try
{
var model = bll_file.GetModel(data);
if (null == model)
{
ret = GetResult(false, "文件记录不存在file_id" + data);
context.Response.Write(ret);
}
else
{
var result = GetResult(model);
context.Response.Write(result);
}
}
catch (Exception ex)
{
var result = GetResult(false, "查询失败,异常:" + ex.Message);
context.Response.Write(result);
}
}
/// <summary>
/// 添加Cos文件 参数 result_id结果id cos_url : cos文件
/// </summary>
/// <param name="context"></param>
public void AddCos(HttpContext context)
{
var ret = string.Empty;
var cos_url = context.Request.Params["cos_url"];
if (string.IsNullOrEmpty(cos_url))
{
ret = GetResult(false, "cos_url参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
var result_id = context.Request.Params["result_id"];
if (string.IsNullOrEmpty(result_id))
{
ret = GetResult(false, "result_id参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
if (null == bll_pro_result.GetModel(result_id))
{
ret = GetResult(false, "考核记录不存在! result_id" + result_id);
context.Response.Write(ret);
context.Response.End();
}
if (null != bll_file.GetModelList(string.Format(" out_id ='{0}' and r1='2' ", result_id)).FirstOrDefault())
{
ret = GetResult(false, "考核文件已上传! result_id" + result_id);
context.Response.Write(ret);
context.Response.End();
}
try
{
var model = new DataService.Model.pro_file();
var newid = BasePage.GetLongId();
var file_key = "File" + newid;
model.file_id = file_key;
model.create_time = DateTime.Now;
model.out_id = result_id;
// file_type 文件类型
/*
model.file_size = file.ContentLength; //文件大小
model.file_desc = file_name; //文件描述
model.file_ext = extension; //文件后缀
model.file_type = extension.Replace(".", ""); //文件类型
*/
var file_size = context.Request.Params["file_size"];
var file_desc = context.Request.Params["file_desc"];
var file_ext = context.Request.Params["file_ext"];
var file_type = context.Request.Params["file_type"];
int isize = 0;
if (int.TryParse(file_size, out isize))
{
model.file_size = isize;
}
model.file_desc = file_desc;
model.file_ext = file_ext;
model.file_type = file_type;
model.file_url = cos_url; //文件路径-相对路径
model.r1 = "2";//上传完成
var flag = bll_file.Add(model);
if (flag)
{
var result = GetResult(true, model, "上传成功!");
context.Response.Write(result);
}
else
{
var result = GetResult(false, model, "添加失败");
context.Response.Write(result);
}
}
catch (Exception ex)
{
var result = GetResult(false, "添加失败,异常:" + ex.Message);
context.Response.Write(result);
}
}
#region
/// <summary>
/// 添加文件 参数 ProgressId进度id file :文件
/// </summary>
/// <param name="context"></param>
public void Add_Lianxi(HttpContext context)
{
var ret = string.Empty;
if (context.Request.Files.Count <= 0)
{
ret = GetResult(false, "文件不能为空");
context.Response.Write(ret);
context.Response.End();
}
var result_id = context.Request.Params["result_id"];
if (string.IsNullOrEmpty(result_id))
{
ret = GetResult(false, "result_id参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
var theory_base_id = context.Request.Params["theory_base_id"];
if (string.IsNullOrEmpty(theory_base_id))
{
ret = GetResult(false, "theory_base_id参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
/*
var no = context.Request.Params["no"];
if (string.IsNullOrEmpty(no))
{
ret = GetResult(false, "no参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
if (null != bll_file.GetModelList(string.Format(" theory_base_id ='{0}' and no={1} and out_id='{2}' ", theory_base_id, no, result_id)).FirstOrDefault())
{
ret = GetResult(false, "文件已上传!");
context.Response.Write(ret);
context.Response.End();
}
*/
try
{
var model = new DataService.Model.pro_file();
//var newid = Path.GetRandomFileName().Replace(".", "");
var newid = BasePage.GetId();
var file_key = "File_" + newid;
model.file_id = file_key;
model.create_time = DateTime.Now;
model.out_id = result_id;
model.theory_base_id = theory_base_id;
//model.no = int.Parse(no);
var file = context.Request.Files[0];
var extension = Path.GetExtension(file.FileName);
var file_name = file.FileName;
string uploadPath = "Upload/";
string NewfileName = newid + extension;
// file_type 文件类型
model.file_size = file.ContentLength; //文件大小
model.file_desc = file_name; //文件描述
model.file_ext = extension; //文件后缀
model.file_type = extension.Replace(".", ""); //文件类型
model.file_url = "~/" + uploadPath + NewfileName; //文件路径-相对路径
model.r1 = "1";//上传中
var flag = bll_file.Add(model);
if (flag)
{
var directory = context.Server.MapPath("~/" + uploadPath);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
string serverPath = directory + NewfileName;
file.SaveAs(serverPath);
/*
var thumbnail_url = "";
if (ImageClass.IsRealImage(serverPath))
{
var thumb_fileName = prefix + "_" + newid + "_r" + extension;
var newPath = uploadPath + thumb_fileName;
var reducePath = directory + thumb_fileName;
ImageClass img = new ImageClass(serverPath);
img.GetReducedImage(300, 300, reducePath);
thumbnail_url = uploadPath + thumb_fileName; //缩略图路径(图片)
}
*/
var item = bll_file.GetModel(file_key);
if (null != item)
{
item.r1 = "2";//上传完成
//item.thumbnail_url = thumbnail_url;
bll_file.Update(item);
}
var Authority = context.Request.Url.Authority;
var new_url = "http://" + Authority + model.file_url.Replace("~", "");
item.http_url = new_url;
var result = GetResult(true, item, "上传成功!");
context.Response.Write(result);
}
else
{
var result = GetResult(false, model, "添加失败");
context.Response.Write(result);
}
}
catch (Exception ex)
{
var result = GetResult(false, "添加失败,异常:" + ex.Message);
context.Response.Write(result);
}
}
/// <summary>
/// 删除练习
/// </summary>
/// <param name="context"></param>
public void Delete_Lianxi(HttpContext context)
{
var ret = string.Empty;
var file_id = context.Request.Params["file_id"];
if (string.IsNullOrEmpty(file_id))
{
ret = GetResult(false, "file_id参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
var model = bll_file.GetModel(file_id);
if (null == model)
{
ret = GetResult(false, "文件记录不存在 file_id:"+ file_id);
context.Response.Write(ret);
context.Response.End();
}
var url = model.file_url;
if (bll_file.Delete(model.file_id))
{
string oldParh = context.Server.MapPath(url);
if (File.Exists(oldParh))
{
File.Delete(oldParh);
}
ret = GetResult(true);
context.Response.Write(ret);
context.Response.End();
}
else
{
ret = GetResult(false, "删除失败 file_id:" + file_id);
context.Response.Write(ret);
context.Response.End();
}
//file_id
/*
var result_id = context.Request.Params["result_id"];
if (string.IsNullOrEmpty(result_id))
{
ret = GetResult(false, "result_id参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
var theory_base_id = context.Request.Params["theory_base_id"];
if (string.IsNullOrEmpty(theory_base_id))
{
ret = GetResult(false, "theory_base_id参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
var no = context.Request.Params["no"];
if (string.IsNullOrEmpty(no))
{
ret = GetResult(false, "no参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
var model = bll_file.GetModelList(string.Format(" theory_base_id ='{0}' and no={1} and out_id='{2}' ", theory_base_id, no, result_id)).FirstOrDefault();
if (null == model)
{
ret = GetResult(false, "文件记录不存在");
context.Response.Write(ret);
context.Response.End();
}
*/
}
/// <summary>
/// 查询练习
/// </summary>
/// <param name="context"></param>
public void Query_Lianxi(HttpContext context)
{
var ret = string.Empty;
var result_id = context.Request.Params["result_id"];
if (string.IsNullOrEmpty(result_id))
{
ret = GetResult(false, "result_id参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
var theory_base_id = context.Request.Params["theory_base_id"];
if (string.IsNullOrEmpty(theory_base_id))
{
ret = GetResult(false, "theory_base_id参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
var query = string.Format(" theory_base_id ='{0}' and out_id='{1}' ", theory_base_id, result_id);
/*
var no = context.Request.Params["no"];
if (!string.IsNullOrEmpty(no))
{
query = query + " and no=" + no + " ";
}
*/
var list = bll_file.GetModelList(query).OrderBy(s => s.create_time).ToList();
List<DataService.Model.pro_file> list_back = new List<DataService.Model.pro_file>();
foreach (var item in list)
{
if (!string.IsNullOrEmpty(item.file_url))
{
try
{
var dir_file = context.Server.MapPath(item.file_url);
if (!File.Exists(dir_file))
{
continue;
}
}
catch(Exception ex)
{
continue;
}
var Authority = context.Request.Url.Authority;
var new_url = "http://" + Authority + item.file_url.Replace("~", "");
item.http_url = new_url;
list_back.Add(item);
}
}
ret = GetResult(true, list_back);
context.Response.Write(ret);
context.Response.End();
}
#endregion
/// <summary>
/// 添加文件 参数 ProgressId进度id file :文件
/// </summary>
/// <param name="context"></param>
public void Add(HttpContext context)
{
var ret = string.Empty;
if (context.Request.Files.Count <= 0)
{
ret = GetResult(false, "文件不能为空");
context.Response.Write(ret);
context.Response.End();
}
//xmnyazzzwhr
//var temp = BasePage.GetMD5("xmnyazzzwhr");
var result_id = context.Request.Params["result_id"];
if (string.IsNullOrEmpty(result_id))
{
ret = GetResult(false, "result_id参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
if (null == bll_pro_result.GetModel(result_id))
{
ret = GetResult(false, "考核记录不存在! result_id" + result_id);
context.Response.Write(ret);
context.Response.End();
}
if (null != bll_file.GetModelList(string.Format(" out_id ='{0}' and r1='2' ", result_id)).FirstOrDefault())
{
ret = GetResult(false, "考核文件已上传! result_id" + result_id);
context.Response.Write(ret);
context.Response.End();
}
try
{
var model = new DataService.Model.pro_file();
var newid = Path.GetRandomFileName().Replace(".", "");
var file_key = "File_" + newid;
model.file_id = file_key;
model.create_time = DateTime.Now;
model.out_id = result_id;
var file = context.Request.Files[0];
var extension = Path.GetExtension(file.FileName);
var file_name = file.FileName;
string uploadPath = "Upload/";
string NewfileName = newid + extension;
// file_type 文件类型
model.file_size = file.ContentLength; //文件大小
model.file_desc = file_name; //文件描述
model.file_ext = extension; //文件后缀
model.file_type = extension.Replace(".", ""); //文件类型
model.file_url = uploadPath + NewfileName; //文件路径-相对路径
model.r1 = "1";//上传中
var flag = bll_file.Add(model);
if (flag)
{
var directory = context.Server.MapPath("~/" + uploadPath);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
string serverPath = directory + NewfileName;
file.SaveAs(serverPath);
/*
var thumbnail_url = "";
if (ImageClass.IsRealImage(serverPath))
{
var thumb_fileName = prefix + "_" + newid + "_r" + extension;
var newPath = uploadPath + thumb_fileName;
var reducePath = directory + thumb_fileName;
ImageClass img = new ImageClass(serverPath);
img.GetReducedImage(300, 300, reducePath);
thumbnail_url = uploadPath + thumb_fileName; //缩略图路径(图片)
}
*/
var item = bll_file.GetModel(file_key);
if (null != item)
{
item.r1 = "2";//上传完成
//item.thumbnail_url = thumbnail_url;
bll_file.Update(item);
}
var result = GetResult(true, item, "上传成功!");
context.Response.Write(result);
}
else
{
var result = GetResult(false, model, "添加失败");
context.Response.Write(result);
}
}
catch (Exception ex)
{
var result = GetResult(false, "添加失败,异常:" + ex.Message);
context.Response.Write(result);
}
}
//修改文件
public void Update(HttpContext context)
{
var data = context.Request.Params["data"];
var ret = string.Empty;
if (string.IsNullOrEmpty(data))
{
ret = GetResult(false, "data参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
try
{
var model = JsonConvert.DeserializeObject<DataService.Model.pro_file>(data);
var flag = bll_file.Update(model);
if (flag)
{
var result = GetResult(true, model);
context.Response.Write(result);
}
else
{
var result = GetResult(false, model, "修改失败");
context.Response.Write(result);
}
}
catch (Exception ex)
{
var result = GetResult(false, "修改失败,异常:" + ex.Message);
context.Response.Write(result);
}
}
/// <summary>
/// 删除缓存的文件
/// </summary>
/// <param name="context"></param>
public void DeleteCacheFiles(HttpContext context)
{
var list = bll_file.GetModelList(" r1 ='1'").ToList();
foreach (var item in list)
{
var flag = bll_file.Delete(item.file_id);
if (flag)
{
try
{
if (!string.IsNullOrEmpty(item.file_url))
{
string path = context.Server.MapPath("~/" + item.file_url);
if (File.Exists(path))
{
File.Delete(path);
}
}
if (!string.IsNullOrEmpty(item.thumbnail_url))
{
string path = context.Server.MapPath("~/" + item.thumbnail_url);
if (File.Exists(path))
{
File.Delete(path);
}
}
}
catch (Exception fileException)
{
write_log("删除文件异常file_url:" + item.file_url + ",thumbnail_url:" + item.thumbnail_url + ",异常:" + fileException.ToString());
}
}
}
}
//删除进度文件记录,同时删除文件 参数 file_id文件id
public void Delete(HttpContext context)
{
var data = context.Request.Params["file_id"];
var ret = string.Empty;
if (string.IsNullOrEmpty(data))
{
ret = GetResult(false, "file_id参数不能为空");
context.Response.Write(ret);
context.Response.End();
}
var model = bll_file.GetModel(data);
if (null == model)
{
ret = GetResult(false, "文件记录不存在file_id" + data);
context.Response.Write(ret);
context.Response.End();
}
try
{
var flag = bll_file.Delete(data);
if (flag)
{
try
{
if (!string.IsNullOrEmpty(model.file_url))
{
string path = context.Server.MapPath("~/" + model.file_url);
if (File.Exists(path))
{
File.Delete(path);
}
}
if (!string.IsNullOrEmpty(model.thumbnail_url))
{
string path = context.Server.MapPath("~/" + model.thumbnail_url);
if (File.Exists(path))
{
File.Delete(path);
}
}
}
catch (Exception fileException)
{
write_log("删除文件异常file_url:" + model.file_url + ",thumbnail_url:" + model.thumbnail_url + ",异常:" + fileException.ToString());
}
DeleteCacheFiles(context);
var result = GetResult(true);
context.Response.Write(result);
}
else
{
var result = GetResult(false, "删除失败");
context.Response.Write(result);
}
}
catch (Exception ex)
{
var result = GetResult(false, "删除失败,异常:" + ex.Message);
context.Response.Write(result);
}
}
public bool IsReusable
{
get
{
return false;
}
}
/// <summary>
/// 查询某进度文件列表接口返回数据
/// </summary>
public class ProgressFileData
{
/// <summary>
/// 项目类型
/// </summary>
public string DateDesc { get; set; }
/// <summary>
/// 项目类型
/// </summary>
public List<DataService.Model.pro_file> data { get; set; }
}
}
}