众所皆知,web上传大文件,一直是一个痛。上传文件大小限制,页面响应时间超时.这些都是web开发所必须直面的。

本文给出的解决方案是:前端实现数据流分片长传,后面接收完毕后合并文件的思路。下面贴出简易DEMO源码分享:

前端页面:

@{

    ViewBag.Title = "Upload";

}

<h2>Upload</h2>

<table class="table table-striped">

    <tr>

        <td><input type="file" id="file"  onchange="selfile()" /></td>

        <td><input type="button" value="上传" onclick="uploading()" /></td>

    </tr>

    <tr>

        <td colspan="2">文件信息:<span id="fileMsg"></span></td>

    </tr>

    <tr>

        <td colspan="2">当前进度:<span id="upsize"></span></td>

    </tr>

</table>

<script src="~/Scripts/myUploader.js"></script>

<script type="text/javascript">

   //guid

    var guid = "@Guid.NewGuid()";

    var uploader;

    function selfile() {

        var f =  $("#file")[0].files[0];

        uploader = new SupperUploader("@Url.Action("RecvUpload")", f, guid, (1024*1024));

        $("#fileMsg").text("文件名:" + uploader.fileName + "文件类型:" + uploader.fileType + "文件大小:" + uploader.fileSize + "字节");

    }

    function uploading() {

        uploader.UploadFun(function () {

            $("#upsize").text(uploader.upedSize);

        })

    }

</script>

 

后端代码,此Demo是基于MVC架构的:

[HttpGet]

public ActionResult Upload() {

return View();

}

[HttpPost]

public ActionResult RecvUpload(){

try

{

string fileName = Request["fname"];

string index = Request["index"];

string guid = Request["guid"];

var file = Request.Files[0];

var ismerge = Request["ismerge"];

string tempDirpath = "~/Content/temp/" + guid + "/";

string savepath = tempDirpath + index + "_" + fileName;

//合并文件

if (bool.Parse(ismerge))

{

//获取所有分割文件

var files = System.IO.Directory.GetFiles(Server.MapPath(tempDirpath));

//文件FILEINFO

var infos = files.Select(x => new FileInfo(x)).ToList().OrderBy(x=>x.LastWriteTime).ToList();

//合并文件流

FileStream mergefs = new FileStream(Server.MapPath("~/Content/temp/" + fileName),FileMode.Append);

BinaryWriter bw = new BinaryWriter(mergefs);

FileStream tempfs = null;

BinaryReader tempbr= null;

infos.ToList().ForEach(f =>

{

tempfs = new FileStream(f.FullName, FileMode.Open);

tempbr = new BinaryReader(tempfs);

bw.Write(tempbr.ReadBytes((int)tempfs.Length));

tempfs.Close();

tempbr.Close();

});

bw.Close();

mergefs.Close();

//删除分块文件

infos.ForEach(f =>{

System.IO.File.Delete(f.FullName);

});

return Json("success");

}

if (!System.IO.Directory.Exists(Server.MapPath(tempDirpath))){

System.IO.Directory.CreateDirectory(Server.MapPath(tempDirpath));

}

using (FileStream fs = new FileStream(Server.MapPath(savepath), FileMode.CreateNew))

{

using (Stream stream = file.InputStream)

{

byte[] buffer = new byte[stream.Length];

stream.Read(buffer, 0, (int)stream.Length);

fs.Write(buffer, 0, buffer.Length);

}

}

return Json("success");

}

catch (Exception e)

{

return Json(e.Message);

}

}

最终效果:

​

上传文件存储服务器目录:

D:\wamp64\www\up6\db\upload\2019\04\19\920144c756af424ca59136be71cf9209



文件上传记录可在数据库中查看:



文件上传完成,没有出现丢包的情况,完全可以直接使用了。
DEMO下载地址:http://blog.ncmem.com/wordpress/2019/08/09/web大文件上传解决方案-2/

WEB上传大文件解决方案的更多相关文章

  1. web上传大文件(>4G)有什么解决方案?

    众所皆知,web上传大文件,一直是一个痛.上传文件大小限制,页面响应时间超时.这些都是web开发所必须直面的. 本文给出的解决方案是:前端实现数据流分片长传,后面接收完毕后合并文件的思路. 实现文件夹 ...

  2. WEB上传大文件

    众所皆知,web上传大文件,一直是一个痛.上传文件大小限制,页面响应时间超时.这些都是web开发所必须直面的. 本文给出的解决方案是:前端实现数据流分片长传,后面接收完毕后合并文件的思路.下面贴出简易 ...

  3. Web上传大文件的解决方案

    需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在500M内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以501M来进行限制. 第一步: 前端修改 由于项目使用的是 ...

  4. java上传大文件解决方案

    需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在10G内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以10G来进行限制. 第一步: 前端修改 由于项目使用的是BJ ...

  5. ASP.Net上传大文件解决方案之IIS7.0下的配置

    开源的Brettle.Web.NeatUpload.在公司IIS6.0使用正常,但是在Windows 2008 server IIS7上使用不正常.在网上看到一个解决办法但是没有效果 IIS 7 默认 ...

  6. Web上传超大文件解决方案

    文件上传下载,与传统的方式不同,这里能够上传和下载10G以上的文件.而且支持断点续传. 通常情况下,我们在网站上面下载的时候都是单个文件下载,但是在实际的业务场景中,我们经常会遇到客户需要批量下载的场 ...

  7. asp.net core mvc上传大文件解决方案

    默认上传文件大小不超过30M 第一个问题: IIS 10.0 详细错误 - 404.13 - Not Found 请求筛选模块被配置为拒绝超过请求内容长度的请求. 服务器上的请求筛选被配置为拒绝该请求 ...

  8. web上传大文件的配置

    1.项目本身的webconfig  在<system.web>字段下 <httpRuntime targetFramework="4.5" requestLeng ...

  9. vue上传大文件的解决方案

    众所皆知,web上传大文件,一直是一个痛.上传文件大小限制,页面响应时间超时.这些都是web开发所必须直面的. 本文给出的解决方案是:前端实现数据流分片长传,后面接收完毕后合并文件的思路. 实现文件夹 ...

随机推荐

  1. elastic5.4安装错误解决

    首先,我们从官网下载:(官网:https://www.elastic.co/downloads/elasticsearch)(推荐下载deb或者rpm包,否则坑很多) 启动 (需要依赖java环境) ...

  2. php5.6 版本出现 Automatically populating $HTTP_RAW_POST_DATA is deprecated and will be removed in a future version 的错误

    解决方法是修改php.ini配置: ;always_populate_raw_post_data = -1 把前面的分号去掉 always_populate_raw_post_data = -1 然后 ...

  3. NuGet打包,IIS自动发布

    NuGet学习笔记(1)——初识NuGet及快速安装使用 https://kb.cnblogs.com/page/143190/ NuGet学习笔记(2)——使用图形化界面打包自己的类库 https: ...

  4. nginx与php-fpm原理

    一.正向代理与反向代理 1.正向代理:访问google.com google.com   vpn需要FQ才能访问 vpn 对于我们来说是可以感知到的(我们连接vpn),但对于google服务器是不知道 ...

  5. 2Q - Fibbonacci Number

    Your objective for this question is to develop a program which will generate a fibbonacci number. Th ...

  6. Hibbernate详解一

    这里先做一个简单的入门,后面有详解 记住图解原理: 这里只是没有整合spring等项目使用的hibernate的使用详解. 一.Hibernate简介 1.Hibernate在开发中所处的位置 2.O ...

  7. sFlow-rt安装部署

      sFlow技术是一种以设备端口为基本单元的数据流随机采样的流量监控技术,不仅可以提供完整的第二层到第四层甚至全网范围内的实时流量信息,而且可以适应超大网络流量(如大于10Gbit/s)环境下的流量 ...

  8. N! (数组)

    #include <iostream> using namespace std; ; int f[MAXN]; int main(){ int n; cin >> n; f[] ...

  9. HDU_2136

    #include <iostream> #include <stdio.h> #include <math.h> #include <algorithm> ...

  10. Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986

    最近在Tomcat上配置一个项目,在点击一个按钮,下载一个文件的时候,老是会报上面的错误.试了很多方法,如对server.xml文件中,增加MaxHttpHeaderSize的大小,改端口,改Tomc ...