.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(0, path.LastIndexOf("/"));
                        byte[] bytes = new byte[stream.Length];
                        stream.Read(bytes, 0, bytes.Length);
                        stream.Seek(0, 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, 0, 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>

 

posted @ 2019-05-15 10:13  小王子的小博客  阅读(373)  评论(0编辑  收藏  举报