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
{
///
/// 通用附件上传处理(多附件保存)
///
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 });
}
}
///
/// 通用附件删除处理
///
public JsonResult Delete(List filepath)
{
try
{
var filepathjson = Request["filepath"];
if (!String.IsNullOrEmpty(filepathjson))
filepath = new JavaScriptSerializer().Deserialize>(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; }
}
}