- 前端代码
- 后端代码
话不多说,上代码(本案例是在新增或修改某些信息时所弹出界面里上传或下载文档的,故将上传input隐藏了,新增或修改时点确定即会上传)! 前端代码
...省略若干代码...后端代码...
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Http;
using System.IO;
using System.Web;
using ******
namespace XX.XX.BASE.Controllers
{
public class ContractController : Controller
{
//文件上传
public ActionResult Upload()
{//E:项目TempContract (我上传的文件放在这个文件夹)
string pathForSaving = Server.MapPath(string.Format("~/{0}", @"TempContract"));
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
for (int i = 0; i < files.Count; i++) {
string type = files[i].FileName.Substring(files[i].FileName.LastIndexOf(".") + 1);
if (type == "doc" || type == "docx" || type == "xls" || type == "xlsx" || type == "ppt" || type == "pptx" || type == "pdf" || type == "jpg" || type == "bmp" || type == "gif" || type == "png" || type == "txt" || type == "zip" || type == "rar")
{
var path = Path.Combine(pathForSaving, files[i].FileName);
files[i].SaveAs(path);
}
}
return Redirect("/#!/BASE/Contract");
}
}
public class ContractApiController : ApiController
{
//文件下载
public string GetDownLoad(RequestWrapper request)
{
string httpurl = "";
//获取下载的文件名
string tempExcelName = request["ContractName"].ToString();
//获取项目绝对路径地址
string url = HttpContext.Current.Request.Url.AbsoluteUri.ToString().Split('/')[0] + "//" + HttpContext.Current.Request.Url.Authority.ToString();
var virtualPath = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath;
string fileName = "";
if (virtualPath != "/")
{
fileName = virtualPath + "/Temp/Contract/" + tempExcelName;
}
else
{
fileName = "/Temp/Contract/" + tempExcelName;
}
httpurl = url + fileName;
return httpurl;
}
}
}



