栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

多文件上传与下载(前端form表单提交后端.net)

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

多文件上传与下载(前端form表单提交后端.net)

多文件上传与下载(前端form表单提交后端.net)
    • 前端代码
    • 后端代码

话不多说,上代码(本案例是在新增或修改某些信息时所弹出界面里上传或下载文档的,故将上传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;
        }
      }
   }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/955618.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号