您现在的位置是:网站首页> 编程资料编程资料
ASP.NET MVC4 利用uploadify.js多文件上传_实用技巧_
2023-05-24
309人已围观
简介 ASP.NET MVC4 利用uploadify.js多文件上传_实用技巧_
页面代码:
1.引入js和css文件
2.body内代码
ASP .NET MVC4 多文件文件上传实例
文件列表
图片列表
后台代码:
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("
" + str + "
"); } else if (exStr == ".doc" || exStr == ".docx") { sb.Append("
" + str + "
"); } else if (exStr == ".ppt" || exStr == ".pptx") { sb.Append("
" + str + "
"); } else if (exStr == ".xlsx" || exStr == ".xls" || exStr == ".XLS") { sb.Append("
" + str + "
"); } else if (exStr == ".pdf") { sb.Append("
" + str + "
"); } else if (exStr == ".js" || exStr == ".JS") { sb.Append("
" + str + "
"); } else if (exStr == ".html" || exStr == ".HTML") { sb.Append("
" + str + "
"); } else if (exStr == ".txt" || exStr == ".TXT") { sb.Append("
" + str + "
"); } else if (exStr == ".mp3" || exStr == ".wmv" || exStr == ".aac") { sb.Append("
" + str + "
"); } else if (exStr == ".avi" || exStr == ".mov" || exStr == ".mp4" || exStr == ".ram" || exStr == ".flv") { sb.Append("
" + str + "
"); } else { sb.Append("
" + str + "
"); } } } 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("
" + str + "
"); } } 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("", Url.Action("Upload")); return Content(script, "text/html"); } } 以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!
您可能感兴趣的文章:
相关内容
- 模拟HTTP请求实现网页自动操作及数据采集的方法_实用技巧_
- WPF实现slide控件拖动完成后改变变量值_实用技巧_
- ASP.NET数据绑定控件详解_实用技巧_
- 详解ASP.NET Core MVC 源码学习:Routing 路由_实用技巧_
- ASP .NET 可编辑输入自动匹配的下拉框_实用技巧_
- Asp .net 调用带参数的存储过程_实用技巧_
- ASP.NET使用ajax实现分页局部刷新页面功能_实用技巧_
- 使用Topshelf组件构建简单的Windows服务_实用技巧_
- ASP.NET导出word实例_实用技巧_
- Visual Studio ASP.NET Core MVC入门教程第一篇_实用技巧_
