MVC中的上传、导出、下载、水印图片
上传页面
<div>
<form action="@Url.Action("FileUpload")" method="post" enctype="multipart/form-data">
Name:<input type="text" name="Name" /><br />
AradeNo:<input type="text" name="AradeNo" /><br />
ClassNo:<input type="text" name="ClassNo" /><br />
Sex:<input type="text" name="Sex" /><br />
Age:<input type="text" name="Age" /><br />
FileName:<input id="File1" type="file" name="ExcFile"/><br />
<input id="Submit1" type="submit" value="上传" />
</form>
</div>
///上传(控制器方法)
public ActionResult FileUpload(string Name,int AradeNo,int ClassNo,string Sex,int Age,HttpPostedFileBase ExcFile)
{
if (ExcFile == null)
{
return View();
}
var filename = Server.MapPath("/EXCEL/");
if (!Directory.Exists(filename))
{
Directory.CreateDirectory(filename);
}
string scFileName = Path.GetFileName(ExcFile.FileName);
string scNewFileName = string.Format("{0}.{1}", Guid.NewGuid().ToString(), scFileName.Split('.')[1]);
ExcFile.SaveAs(filename + scNewFileName);
StudentModel m = new StudentModel();
m.Name = Name;
m.AradeNo = AradeNo;
m.ClassNo = ClassNo;
m.Sex = Sex;
m.Age = Age;
m.FileName = scFileName;
m.NewFileName = scNewFileName;
var result = new BLL.StudentBLL().ShangChuan(m);
if (result > 0)
{
return Content("<script>alert('上传成功!');location.href='/Home/ShowList'</script>");
}
else
{
return Content("<script>alert('上传失败!')</script>");
}
}
///导出(控制器方法)(记得引用NPOI插件)
public FileResult DaoChu()
{
var result = new BLL.StudentBLL().Show();
if (result != null || result.Count > 0)
{
HSSFWorkbook book = new HSSFWorkbook();
ISheet sheet = book.CreateSheet("sheet1");
IRow row = sheet.CreateRow(0);
row.CreateCell(0).SetCellValue("Name");
row.CreateCell(1).SetCellValue("AradeNo");
row.CreateCell(2).SetCellValue("ClassNo");
row.CreateCell(3).SetCellValue("Sex");
row.CreateCell(4).SetCellValue("Age");
row.CreateCell(5).SetCellValue("FileName");
for (int i = 0; i < result.Count; i++)
{
IRow rows = sheet.CreateRow(i + 1);
rows.CreateCell(0).SetCellValue(result[i].Name);
rows.CreateCell(1).SetCellValue(result[i].AradeNo);
rows.CreateCell(2).SetCellValue(result[i].ClassNo);
rows.CreateCell(3).SetCellValue(result[i].Sex);
rows.CreateCell(4).SetCellValue(result[i].Age);
rows.CreateCell(5).SetCellValue(result[i].FileName);
}
MemoryStream ms = new MemoryStream();
book.Write(ms);
ms.Seek(0, SeekOrigin.Begin);
return File(ms, "application/ved-excel", "信息.xls");
}
else
{
MemoryStream ms = new MemoryStream();
ms.Seek(0, SeekOrigin.Begin);
return File(ms, "application/ved-excel", "信息.xls");
}
}
///下载(控制器方法)(使用id传值)
public ActionResult XiaZai(int id)
{
List<InfoModel> m = new BLL.InfoBLL().showinfoID(id);
InfoModel f = new InfoModel();
f.SCNewName = m[0].SCNewName;
string strfileName = Server.MapPath("/EXCEL/"); //上传的路径 strfileName路径名
FileStream fs = new FileStream(string.Format("{0}{1}", strfileName, f.SCNewName), FileMode.Open);
return File(fs, "application/vnd.ms-excel", f.SCName);
}
//水印图片
<div>
<form action="/Home/ShuiYing" method="post" enctype="multipart/form-data">
<input name="File1" type="file" />
<input id="Submit1" type="submit" value="提交" />
</form>
</div>(页面)
public ActionResult ShuiYing(HttpPostedFileBase File1)
{
string warning = "";
string files = Server.MapPath(File1.FileName);
string filename = files.Remove(files.LastIndexOf("\\") - 4) + "img\\" + File1.FileName;
string watchname = files.Remove(files.LastIndexOf("\\") - 4) + "img\\" + "1.jpg";
File1.SaveAs(filename);
string WarkUlr = WatherHelper.makewatermark(filename, watchname, WaterType.Center, files.Remove(files.LastIndexOf("\\") - 4) + "img\\", ImageType.JPEG, FileCache.Delete, out warning);
return Content(WarkUlr);
}
转载于:https://www.cnblogs.com/jpp666/p/7883089.html