如果我是DJ,是DJ,是DJ,是DJ,是DJ,是DJ,是DJ,是DJ,是DJ,是DJ,,,

前言

文件夹上传目前仅支持chrome内核的浏览器。

后期整理到git(2019-5-23说:不整理了,我要干大事去了,撒由那拉~)

前端代码:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>uploadFile</title>
</head>
<body>
<form action="http://localhost:33378/api/v1/czcf/folder/folder_upload" enctype="multipart/form-data" method="post">
<input type="hidden" name="type" value="1"/>
<input id="dir" type="file" name="file" webkitdirectory mozdirectory/>
<input id="uploadDir" type="submit" value="提交文件夹">
</form>
</body>
</html>

后端代码:

        [HttpPost, Route("folder_upload")]
public async Task<string> FolderUpload()
{
string root = HttpContext.Current.Server.MapPath("~/App_Data");
try
{
var multipartMemoryStreamProvider = await Request.Content.ReadAsMultipartAsync();
return await FolderUploadAsync(multipartMemoryStreamProvider, root);
}
catch (Exception e)
{
return "失败:" +e.Message;
}
}
public async Task<string> FolderUploadAsync(MultipartMemoryStreamProvider multipartMemoryStreamProvider,string root)
{
foreach (var content in multipartMemoryStreamProvider.Contents)
{
//通过判断fileName是否为空,判断是否为文件类型
if (!string.IsNullOrEmpty(content.Headers.ContentDisposition.FileName))
{
string fileName = content.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);
using (Stream stream = await content.ReadAsStreamAsync())
{
string path = root + @"\" + fileName;
path = path.Substring(, path.LastIndexOf("/"));
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, , bytes.Length);
stream.Seek(, SeekOrigin.Begin); if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
if (File.Exists(root + @"\" + fileName))
{
File.Delete(root + @"\" + fileName);
} FileStream fs = new FileStream(root + @"\" + fileName, FileMode.Create);
//开始写入
fs.Write(bytes, , bytes.Length);
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
//CommonUtils.SaveFile(bytes, root + @"\" + fileName);//保存文件
}
}
}
return "ok";
}

上传文件过大的话,有可能会出现iis报错

需要修改web.config文件

<system.web>
<compilation debug="true" targetFramework="4.5">
<assemblies />
</compilation>
<httpRuntime targetFramework="4.5" maxRequestLength="2147483647" /> <!--最大2G-->
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483647"/><!--最大2G-->
</requestFiltering>
</security>
</system.webServer>

最新文章

  1. Java设计模式之单例模式(Singleton)
  2. AE开发实现GP工具IDW
  3. iOS阶段学习第31天笔记(UINavigationBar介绍)
  4. 使用HttpDownLoadHelper下载文件
  5. D3D标注动态避让
  6. jQuery UI 实例 - 对话框(Dialog)(zhuan)
  7. GPS之NMEA协议20160526
  8. node搜索codeforces 3A - Shortest path of the king
  9. [转]Permission denied: /.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable
  10. oracle flashback 2
  11. php 设置白名单ip
  12. Python中的list,tuple,dict,set
  13. ubuntu系统内核替换
  14. Akka(6): become/unbecome:运算行为切换
  15. 機器學習基石(Machine Learning Foundations) 机器学习基石 课后习题链接汇总
  16. Spring通过注释配置Bean2 关联关系
  17. python 中@ 的用法【转】
  18. sass基础学习(一)
  19. C#:进程
  20. Jquery实现简单选项卡

热门文章

  1. Servlet身份验证过滤器
  2. docker+jenkins的实现方式(ps.使用dockerfile的方式)!
  3. FinalShell for Mac
  4. 2013年山东省第四届ACM大学生程序设计竞赛E题:Alice and Bob
  5. C++异常相关
  6. JAVA内存溢出解析(转)
  7. @bzoj - 2388@ 旅行规划
  8. H3C Telnet配置例子
  9. jq获取单选框、复选框、下拉菜单的值
  10. supersockets和 AppSession,AppServer 配合工作