You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.8 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
namespace DSWeb.SoftMng.Controllers
{
public class FileInputHandlerController : Controller
{
/// <summary>
/// 通用附件上传处理(多附件保存)
/// </summary>
public JsonResult Upload()
{
try
{
ArrayList pathlist = new ArrayList();
for (var i=0;i< Request.Files.Count;i++)
{
HttpPostedFileBase uploadFile = Request.Files[i];
if (uploadFile != null && uploadFile.ContentLength > 0)
{
var relativepath = "../../UploadFiles/Filepuload/" + DateTime.Now.ToString("yyyyMM");//保存的相对路径
var path = Server.MapPath(relativepath);//获取物理路径(按年月创建文件夹分类)
if (!Directory.Exists(path)) Directory.CreateDirectory(path);
var extension = Path.GetExtension(uploadFile.FileName);
var filepath = "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + new Random().Next(100, 999) + extension;//随机生成文件名
path += filepath;
if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
uploadFile.SaveAs(path);
pathlist.Add(relativepath + filepath);//相对路径数组
}
}
return Json(new { success = true, data = pathlist });
}
catch (Exception se)
{
return Json(new { success = false, msg = se.Message });
}
}
/// <summary>
/// 通用附件删除处理
/// </summary>
public JsonResult Delete(List<FileClass> filepath)
{
try
{
var filepathjson = Request["filepath"];
if (!String.IsNullOrEmpty(filepathjson))
filepath = new JavaScriptSerializer().Deserialize<List<FileClass>>(filepathjson);
foreach (var item in filepath)
{
var path = Server.MapPath(item.url);//获取物理路径
if (System.IO.File.Exists(path)) System.IO.File.Delete(path);
}
return Json(new { success = true, msg = "删除成功" });
}
catch (Exception se)
{
return Json(new { success = false, msg = se.Message });
}
}
}
public class FileClass
{
public string key { get; set; }
public long size { get; set; }
public string url { get; set; }
}
}