怎么在ASP.NET MVC4项目中利用uploadify.js实现一个文件上传功能

发布时间:2020-12-22 15:30:18 作者:Leah
来源:亿速云 阅读:167

本篇文章给大家分享的是有关怎么在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;
  }
  h3, h4 {
   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 >
 <h3 >ASP .NET MVC4 多文件文件上传实例</h3>
 <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">
   <h4>文件列表</h4>
  </div>
  <div id="lineDiv"></div>

  <div id="imglist">
   <h4>图片列表</h4>
  </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.js实现一个文件上传功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. ASP.NET MVC4 捆绑(Bundle)技术下的 JavaScript
  2. 在Asp.net的MVC中如何利用swupload实现多图片上传功能的案例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net uploadify fy

上一篇:使用Go语言怎么快速搭建一个WebSocket服务端

下一篇:使用node.js怎么快速的搭建一个HTTP服务器

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》