全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

ASP.NET MVC4 利用uploadify.js多文件上传

页面代码:

1.引入js和css文件

  <link href="~/Scripts/uploadify/uploadify.css" rel="external nofollow" rel="stylesheet" />
  <style type="text/css">
  #upDiv {
   width: 550px;
   height: 400px;
   border: 2px solid red;
   margin-top: 30px;
   margin-left: 50px;
   float: left;
  }
  div form {
   text-align: center;
   vertical-align: middle;
  }
  h2, h3 {
   text-align: center;
   color: #00B2EE;
  }
  #upList {
   width: 900px;
   height: 400px;
   float: left;
   margin-top: 30px;
   margin-left: 50px;
   overflow-y: scroll;
   border: 2px solid red;
  }
  #filelist {
   width: 45%;
   height: 400px;
   float: left;
  }
  #lineDiv {
   width: 50px;
   height: 400px;
   float: left;
  }
  #imglist {
   width: 45%;
   height: 400px;
   float: left;
  }
  #form1 {
   margin-top: 25px;
  }
  img {
   width: 25px;
   height: 25px;
  }
  .btn {
   width: 150px;
   height: 40px;
   text-align: center;
   background-color: #b58061;
   color: white;
  }
  p {
   cursor: pointer;
  }
 </style>
 <script src="~/Scripts/jquery-1.8.2.min.js"></script>
 <script src="~/Scripts/uploadify/jquery.uploadify-3.1.js"></script>
 <script type="text/javascript">
  $(function () {
   $("#myfile").uploadify({
    "auto": false,
    "swf": "../Scripts/uploadify/uploadify.swf",
    "uploader": "../Home/UploadFiles",
    "removeCompleted": false,
    "onUploadSuccess": function (file, data, response) {
    },
    "onQueueComplete": function () {
     window.location.reload();
    }
   });
   $.ajax({
    url: "/home/loadFileInfo",
    datatype: 'html',
    success: function (result) {
     $('#filelist').append(result);
    }
   });
   $.ajax({
    url: "/home/loadImgInfo",
    datatype: 'html',
    success: function (result) {
     $('#imglist').append(result);
    }
   });
  });
  //在线打开文件
  function openFile(doc) {
   try {
    var fileName = $(doc).text();
    var url = window.location.protocol + "//" + window.location.host + "/UploadFile/File/"
    url = url + fileName;
    window.open(url);
   } catch (EventException) {
    alert("此文件无法打开!");
   }
  }
  //在线打开图片
  function openImg(doc) {
   var fileName = $(doc).text();
   var url = window.location.protocol + "//" + window.location.host + "/UploadImg/Img/"
   url = url + fileName;
   window.open(url);
  }
 </script>

2.body内代码

  <body style="background: url(../../Images/bg.jpg) no-repeat; background-size: 1600px; width: 1600px; height: 700px; ">
 <h2 style="text-align:center;">ASP .NET MVC4 多文件文件上传实例</h2>
 <form id="form1">
  <div>
   <input type="file" id="myfile" name="myfile" />
  </div>
  <div>
   <a class="btn" href="javascript:$('#myfile').uploadify('upload');" rel="external nofollow" >上传第一个</a>
   <a class="btn" href="javascript:$('#myfile').uploadify('upload','*');" rel="external nofollow" >上传队列</a>
   <a class="btn" href="javascript:$('#myfile').uploadify('cancel');" rel="external nofollow" >取消第一个</a>
   <a class="btn" href="javascript:$('#myfile').uploadify('cancel', '*');" rel="external nofollow" >取消队列</a>
  </div>
 </form>
 <div id="upList">
  <div id="filelist">
   <h3>文件列表</h3>
  </div>
  <div id="lineDiv"></div>

  <div id="imglist">
   <h3>图片列表</h3>
  </div>
 </div>
</body>

后台代码:

public ActionResult loadFileInfo()
  {
   StringBuilder sb = new StringBuilder();
   DirectoryInfo theFolder = new DirectoryInfo(Server.MapPath("~/UploadFile/"));
   DirectoryInfo[] dirInfo = theFolder.GetDirectories();
   //遍历文件夹
   foreach (DirectoryInfo NextFolder in dirInfo)
   {
    FileInfo[] fileInfo = NextFolder.GetFiles();
    //遍历文件
    foreach (FileInfo NextFile in fileInfo)
    {
     string exStr = NextFile.Extension;
     string str = NextFile.Name;
     if (exStr == ".zip" || exStr == ".7z" || exStr == ".rar" || exStr.ToLower() == ".rars")
     {
      sb.Append("<p onclick='openFile(this)'><img src='../../Images/zip.png' width='25' height='25' />" + str + "</p>");
     }
     else if (exStr == ".doc" || exStr == ".docx")
     {
      sb.Append("<p onclick='openFile(this)'><img src='../../Images/words.png' width='25' height='25' />" + str + "</p>");
     }
     else if (exStr == ".ppt" || exStr == ".pptx")
     {
      sb.Append("<p onclick='openFile(this)'><img src='../../Images/ppt.jpg' width='25' height='25' />" + str + "</p>");
     }
     else if (exStr == ".xlsx" || exStr == ".xls" || exStr == ".XLS")
     {
      sb.Append("<p onclick='openFile(this)'><img src='../../Images/excel.png' width='25' height='25' />" + str + "</p>");
     }
     else if (exStr == ".pdf")
     {
      sb.Append("<p onclick='openFile(this)'><img src='../../Images/pdf.jpg' width='25' height='25' />" + str + "</p>");
     }
     else if (exStr == ".js" || exStr == ".JS")
     {
      sb.Append("<p onclick='openFile(this)'><img src='../../Images/js.png' width='25' height='25' />" + str + "</p>");
     }
     else if (exStr == ".html" || exStr == ".HTML")
     {
      sb.Append("<p onclick='openFile(this)'><img src='../../Images/html.png' width='25' height='25' />" + str + "</p>");
     }
     else if (exStr == ".txt" || exStr == ".TXT")
     {
      sb.Append("<p onclick='openFile(this)'><img src='../../Images/txt.png' width='25' height='25' />" + str + "</p>");
     }
     else if (exStr == ".mp3" || exStr == ".wmv" || exStr == ".aac")
     {
      sb.Append("<p onclick='openFile(this)'><img src='../../Images/mp3.png' width='25' height='25' />" + str + "</p>");
     }
     else if (exStr == ".avi" || exStr == ".mov" || exStr == ".mp4" || exStr == ".ram" || exStr == ".flv")
     {
      sb.Append("<p onclick='openFile(this)'><img src='../../Images/video.png' width='25' height='25' />" + str + "</p>");
     }
     else {
      sb.Append("<p onclick='openFile(this)'><img src='../../Images/file.jpg' width='25' height='25' />" + str + "</p>");
     }
    }
   }
   return Content(sb.ToString());
  }
  public ActionResult loadImgInfo()
  {
   StringBuilder sb = new StringBuilder();
   DirectoryInfo theFolder = new DirectoryInfo(Server.MapPath("~/UploadImg/"));
   DirectoryInfo[] dirInfo = theFolder.GetDirectories();
   //遍历文件夹
   foreach (DirectoryInfo NextFolder in dirInfo)
   {
    FileInfo[] fileInfo = NextFolder.GetFiles();
    //遍历文件
    foreach (FileInfo NextFile in fileInfo)
    {
     string str = NextFile.Name;
     sb.Append("<p onclick='openImg(this)'><img src='../../Images/img.png' width='25' height='25' />" + str + "</p>");
    }
   }
   return Content(sb.ToString());
  }
  public ActionResult UploadFile()
  {
   string filepath = "";
   bool fileOK = false;
   //判断是否已经选择上传文件
   HttpPostedFileBase file = Request.Files["myfile"];
   if (file != null && file.ContentLength > 0)
   {
    String fileExtension = System.IO.Path.GetExtension(file.FileName).ToLower();
    //判断是否为图片类型
    String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
    for (int i = 0; i < allowedExtensions.Length; i++)
    {
     if (fileExtension == allowedExtensions[i])
     {
      fileOK = true;
     }
    }
    if (fileOK)
    {
     //设置上传目录
     string path = Server.MapPath("~/UploadImg/Img/");
     if (!Directory.Exists(path))
      Directory.CreateDirectory(path);
     string filenNamer = file.FileName;
     //文件路径
     filepath = path + filenNamer;
     file.SaveAs(filepath);
     return RedirectToAction("Upload", "Home");
    }
    else
    {
     //设置上传目录
     string path = Server.MapPath("~/UploadFile/File/");
     if (!Directory.Exists(path))
      Directory.CreateDirectory(path);
     //不为图片类型的文件存入到File目录中
     string filenNamer = file.FileName;
     //文件路径
     filepath = path + filenNamer;
     file.SaveAs(filepath);
     return RedirectToAction("Upload", "Home"); 
    }
   }
   else
   {
    var script = String.Format("<script>alert('请选择文件后再上传!');location.href='{0}'</script>", Url.Action("Upload"));
    return Content(script, "text/html");
   }
  }

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!


# asp.net  # mvc4上传文件  # uploadify多文件上传  # uploadify  # 上传文件  # ASP.NET mvc4中的过滤器的使用  # [Asp.Net MVC4]验证用户登录实现实例  # Asp.Net Core配置多环境log4net配置文件的全过程  # ASP.NET Core使用Log4net实现日志记录功能  # asp.net中使用log4net详解  # ASP.NET MVC中使用log4net的实现示例  # log4net在Asp.net MVC4中的使用过程  # 遍历  # 上传  # 第一个  # 判断是否  # 请选择  # 不为  # 文件列表  # 再上  # 无法打开  # 文件上传  # 目录中  # javascript  # function  # swf  # myfile  # false  # Home  # uploader  # auto 


相关文章: 官网自助建站平台指南:在线制作、快速建站与模板选择全解析  ppt在线制作免费网站推荐,有什么下载免费的ppt模板网站?  北京制作网站的公司,北京铁路集团官方网站?  标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?  如何在搬瓦工VPS快速搭建网站?  自助网站制作软件,个人如何自助建网站?  教育培训网站制作流程,请问edu教育网站的域名怎么申请?  如何在服务器上配置二级域名建站?  建站之星后台管理系统如何操作?  如何通过VPS建站无需域名直接访问?  长沙做网站要多少钱,长沙国安网络怎么样?  如何在云主机上快速搭建网站?  如何配置WinSCP新建站点的密钥验证步骤?  如何选择最佳自助建站系统?快速指南解析优劣  教程网站设计制作软件,怎么创建自己的一个网站?  ,想在网上投简历,哪几个网站比较好?  实例解析angularjs的filter过滤器  怎么将XML数据可视化 D3.js加载XML  香港服务器网站搭建教程-电商部署、配置优化与安全稳定指南  如何高效配置香港服务器实现快速建站?  零服务器AI建站解决方案:快速部署与云端平台低成本实践  大学网站设计制作软件有哪些,如何将网站制作成自己app?  网站制作服务平台,有什么网站可以发布本地服务信息?  定制建站如何定义?其核心优势是什么?  常州自助建站工具推荐:低成本搭建与模板选择技巧  义乌企业网站制作公司,请问义乌比较好的批发小商品的网站是什么?  武汉外贸网站制作公司,现在武汉外贸前景怎么样啊?  高防服务器如何保障网站安全无虞?  学校建站服务器如何选型才能满足性能需求?  在线流程图制作网站手机版,谁能推荐几个好的CG原画资源网站么?  网站制作公司广州有几家,广州尚艺美发学校网站是多少?  测试制作网站有哪些,测试性取向的权威测试或者网站?  建站之星北京办公室:智能建站系统与小程序生成方案解析  TestNG的testng.xml配置文件怎么写  重庆市网站制作公司,重庆招聘网站哪个好?  如何配置IIS站点权限与局域网访问?  定制建站方案优化指南:企业官网开发与建站费用解析  如何选择网络建站服务器?高效建站必看指南  深圳网站制作平台,深圳市做网站好的公司有哪些?  如何彻底卸载建站之星软件?  如何使用Golang table-driven基准测试_多组数据测量函数效率  如何快速搭建安全的FTP站点?  rsync同步时出现rsync: failed to set times on “xxxx”: Operation not permitted  如何在西部数码注册域名并快速搭建网站?  制作网站的基本流程,设计网站的软件是什么?  网站制作外包价格怎么算,招聘网站上写的“外包”是什么意思?  建站之星备案流程有哪些注意事项?  如何在七牛云存储上搭建网站并设置自定义域名?  安云自助建站系统如何快速提升SEO排名?  Thinkphp 中 distinct 的用法解析 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。