.net webapi 文件夹上传
如果我是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>
.net webapi 文件夹上传的更多相关文章
- Linux 本地文件或文件夹上传服务器
Linux 本地文件或文件夹上传服务器 一.权限设置 本地文件或文件夹上传服务器,你首先需要获取到root权限: 二.上传方式 上传方式有两种 : 1.通过 FTP 客户端上传文件或文件夹: 2.通过 ...
- html5 实现 文件夹上传
先插个背景:最近所在项目有个小需求,就是上传文件要可以同时选择文件夹及文件,然后把文件夹内得文件及所选单文件全部选择上传,借助于搜索关键词没搜到想要的结果(相关文章貌似很好,要么就是遍历文件夹内的文件 ...
- java web 实现文件夹上传(保留目录结构)
今天我弄了一下文件夹上传(很简单的 首先,我们的html需要这样写 <form action="/file/upload" enctype="multipart/f ...
- Web大文件(夹)上传(断点续传)控件发布-Xproer.HttpUploader6
版权所有 2009-2017荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...
- Web大文件(夹)上传(断点续传)控件-Xproer.HttpUploader6
版权所有 2009-2017荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...
- webkitdirectory 实现文件夹上传
文件夹上传这个功能在web端可能有需求,这里就简单介绍下用法. 目前只有谷歌浏览器还有Microsoft Edge支持按照文件夹进行上传,具体可以看下百度云盘的网页版的上传按钮,在火狐下就支持按照文件 ...
- vue文件夹上传组件选哪个好?
一. 功能性需求与非功能性需求 要求操作便利,一次选择多个文件和文件夹进行上传:支持PC端全平台操作系统,Windows,Linux,Mac 支持文件和文件夹的批量下载,断点续传.刷新页面后继续传输. ...
- 文件夹上传插件webupload插件
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 先说下要求: PC端全平台支持,要求支持Windows,Mac,Linux 支持所 ...
- 文件夹上传控件webupload插件
我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...
随机推荐
- map的三种遍历方法!
map的三种遍历方法! 集合的一个很重要的操作---遍历,学习了三种遍历方法,三种方法各有优缺点~~ /* * To change this template, choose Tools | Te ...
- UVA_488:Triangle Wave
PS:The input begins with a single positive integer on a line by itself indicating the number of the ...
- importError: DLL load failed when import matplotlib.pyplot as plt
importError: DLL load failed when import matplotlib.pyplot as plt 出现这种情况的原因, 大多是matplotlib的版本与python ...
- 手写call,bind,apply
//实现call var that = this ; //小程序环境 function mySymbol(obj){ let unique = (Math.random() + new Date(). ...
- Plupload的上传机制
plupload支持多文件上传.经过测试发现,plupload在上传多个文件时,会把多个文件拆分成单个的一个一个上传.
- HDU 5672 String【尺取法】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5672 题意: 有一个10≤长度≤1,000,000的字符串,仅由小写字母构成.求有多少个子串,包含有 ...
- POJ-3186_Treats for the Cows
Treats for the Cows Time Limit: 1000MS Memory Limit: 65536K Description FJ has purchased N (1 <= ...
- Python基础:04映射类型
字典是Python语言中唯一的映射类型.一个字典对象是可变的,它是一个容器类型,能存储任意个数的Python对象.字典中的数据是无序排列的. 映射类型也可被称做哈希表,哈希表的算法是获取键,对键执行一 ...
- GitOps:Kubernetes多集群环境下的高效CICD实践
为了解决传统应用升级缓慢.架构臃肿.不能快速迭代.故障不能快速定位.问题无法快速解决等问题,云原生这一概念横空出世.云原生可以改进应用开发的效率,改变企业的组织结构,甚至会在文化层面上直接影响一个公司 ...
- C++:只用初始化列表初始化变量的几种情况
1.类成员函数中const变量的初始化(也就是第一点) 有几个容易混淆的地方: (1)const 的变量只能通过构造函数的初始化列表进行初始化:(貌似在C++11中可以正常编译) (2)static ...