当前位置: 首页 > news >正文

达州网站开发如何制作网页设计

达州网站开发,如何制作网页设计,泉州网站建设公司,响应式网站建设系统大文件上传 实现思路 对于大文件上传考虑到上传时间太久、超出浏览器响应时间、提高上传效率、优化上传用户体验等问题进行了深入探讨,以下初略罗列各个知识点的实现思路: 大文件上传对文件本身进行了文件流内容 Blob 的分割,使用 Blob.pr…

大文件上传

实现思路

对于大文件上传考虑到上传时间太久、超出浏览器响应时间、提高上传效率、优化上传用户体验等问题进行了深入探讨,以下初略罗列各个知识点的实现思路:

  1. 大文件上传对文件本身进行了文件流内容 Blob 的分割,使用 Blob.prototype.slice 实现大文件的上传切分为多个小文件的上传
  2. 为了实现大文件上传能否做到秒传、辨别是否已存在、文件切片的秒传等功能,需要对大文件进行计算 Hash 的唯一标识,通过使用 WebWorker 开启浏览器线程来计算文件 Hash,防止阻塞 UI 渲染(另外也采用 React Fiber 所用的时间分片思想方式 requestIdleCallback API 来计算)
  3. 上传暂停/恢复功能采用 XMLHttpRequest 请求带有的 abort 方法进行请求的取消来实现
  4. 判断文件是否已存在,在性能上可以通过计算抽样 Hash 来大大缩短大文件全量计算 Hash 的时间,使用这个抽样 Hash 向服务器确认是否已存在文件,而达到秒传的功能,抽样 Hash 的作用在于牺牲一点点的识别率来换取时间
  5. 大文件切分为小文件后,通过设置一个上传通道限制,实现控制并发上传数来防止一次性过多的 HTTP 请求而卡死浏览器
  6. 文件切片上传采用请求 catch 捕获方式,来对上传失败的内容进行重试,重试三次后再失败就进行放弃
  7. 对文件服务器过期的文件切片开启定时器清理,采用了 node-schedule 来实现

上传切片

<!-- 单选文件 -->
<input id="fileInput" type="file" />
const fileInput = document.querySelector('#fileInput');// 1. 点击输入框选择文件后触发
fileInput.addEventListener('change', e => {const [file] = e.target.files;if (!file) return;const chunkList = sliceFileChunk(file);
});// 2. 文件切片
function sliceFileChunk(file) {// 文件大小const FILE_SIZE = file.size;// 文件切片大小const CHUNK_SIZE = 2 * 1024 * 1024;// 切片的个数const CHUNKS = Math.ceil(FILE_SIZE / CHUNK_SIZE);const blobSlice = Fil.prototype.slice || File.prototype.mozSlice || File.prototype.webkitSlice;// 生成 MD5const spark = new SparkMD5.ArrayBuffer();// 实例化读取文件对象const reader = new FileReader();const currentChunk = 0;reader.onload = function(e) {const resul = e.target.result;spark.append(result);currentChunk++;if (currentChunk < chunks) {loadNext();console.log(`${currentChunk}个分片解析完成`);} else {const md5 = spark.end();console.log('解析完成');}};function loadNext() {const start = currentChunk * CHUNK_SIZE;const end = start + CHUNK_SIZE > file.size ? file.size : start + CHUNK_SIZE;reader.raedAsArrayBuffer(blobSlice.call(file, start, end));}loadNext();
}// 上传切片
async function uploadChunkus() {const requestList = this.data.map(({ chunk, hash }) => {const formData = new FormData();formData.append('chunk', chunk);formData.append('hash', hash);formData.append('filename', this.container.file.name);return { formData };}).map(async ({ formData }) => {return this.request({url: 'http://localhost:3000',data: formData,});});// 并发上传文件切片await Promise.all(requestList);
}async function handleUpload() {}

发送合并请求

接受切片

const http = require('http');
const path = require('path');
const fse = require('fs-extra');
const multiparty = require('multiparty');const server = http.createServer();
// 大文件存储目录
const UPLOAD_DIR = path.resolve(__dirname, '..', 'target');server.on('request', async (req, res) => {res.setHeader('Access-Control-Allow-Oriign', '*');res.setHeader('Access-Control-Allow-Headers', '*');if (req.method === 'OPTIONS') {res.status = 200;res.end();return;}const multipart = new multiparty.Form();multipart.parse(req, async (err, fields, files) => {if (err) return;const [chunk] = files.chunk;const [hash] = fields.hash;const [filename] = fields.filename;const chunkDir = path.resolve(UPLOAD_DIR, filename);// 切片目录不存在,创建切片目录if (!fse.existsSync(chunkDir)) {await fse.mkdirs(chunkDir);}// fs-extra 专用方法,类似 fs.rename 并且跨平台// fs-extra 的 rename 方法 windows 平台会有权限问题await fse.move(chunk.path, `${chunkDir}/${hash}`);res.end('Received file chunk');});
});server.listen(3000, () => console.log('Server is listening port 3000.'));

合并切片

由于前端在发送合并请求时会携带文件名,服务端根据文件名可以找到上一步创建的切片文件夹。

接着使用 fs.createWriteStream 创建一个可写流,可写流文件名就是 切片文件夹名 + 后缀名 组合而成。

随后遍历整个切片文件夹,将切片通过 fs.createReadStream 创建可读流,传输合并到目标文件中。

值得注意的是每次可读流都会传输到可写流的指定位置,这是通过 createWriteStream 的第二个参数 start/end 控制的,目的是能够并发合并多个可读流到可写流中,这样即使流的顺序不同也能传输到正确的位置,所以这里还需要让前端在请求的时候多提供一个 size 参数。

断点续传

断点续传的原理在于前端/服务端需要 记住 已上传的切片,这样下次上传就可以跳过之前已上传的部分,有两种方案实现记忆的功能:

  • 前端使用 localStorage 记录已上传的切片 hash
  • 服务端保存已上传的切片 hash,前端每次上传前向服务端获取已上传的切片

生成标识

无论是前端还是服务端,都必须要生成文件和切片的 Hash,之前我们使用 文件名 + 切片下标 作为切片 Hash,这样做文件名一旦修改就失去了效果,而事实上只要文件内容不变,Hash 就不应该变化,所以正确的做法是根据文件内容生成 hash,所以我们修改一下 Hash 的生成规则。


文章转载自:
http://dinncoduodena.wbqt.cn
http://dinncofundamentalist.wbqt.cn
http://dinncovrd.wbqt.cn
http://dinncoavionics.wbqt.cn
http://dinncofowling.wbqt.cn
http://dinncoscholarly.wbqt.cn
http://dinncotrajectory.wbqt.cn
http://dinncoullage.wbqt.cn
http://dinncohielamon.wbqt.cn
http://dinncocurettage.wbqt.cn
http://dinncoboehm.wbqt.cn
http://dinncobookselling.wbqt.cn
http://dinncobeton.wbqt.cn
http://dinncoergosterol.wbqt.cn
http://dinncofantod.wbqt.cn
http://dinncolone.wbqt.cn
http://dinncoyup.wbqt.cn
http://dinncoredemptive.wbqt.cn
http://dinncoindifferentism.wbqt.cn
http://dinncogarbageology.wbqt.cn
http://dinncoromaine.wbqt.cn
http://dinncocombined.wbqt.cn
http://dinncotzarina.wbqt.cn
http://dinncodudley.wbqt.cn
http://dinncomotivational.wbqt.cn
http://dinncoboost.wbqt.cn
http://dinncobusybody.wbqt.cn
http://dinncopelicanry.wbqt.cn
http://dinncobilobed.wbqt.cn
http://dinncomalay.wbqt.cn
http://dinncocredibility.wbqt.cn
http://dinncoobviosity.wbqt.cn
http://dinncoundefiled.wbqt.cn
http://dinncomendicity.wbqt.cn
http://dinncocellaret.wbqt.cn
http://dinncocystotomy.wbqt.cn
http://dinncoberat.wbqt.cn
http://dinncofinegrained.wbqt.cn
http://dinncocarnificial.wbqt.cn
http://dinncoradiodetector.wbqt.cn
http://dinncofortifier.wbqt.cn
http://dinncoimperceivable.wbqt.cn
http://dinncotrigeminus.wbqt.cn
http://dinncochlamydomonas.wbqt.cn
http://dinncothymey.wbqt.cn
http://dinncobanka.wbqt.cn
http://dinncosongfest.wbqt.cn
http://dinncopantisocracy.wbqt.cn
http://dinncofisher.wbqt.cn
http://dinncocaplin.wbqt.cn
http://dinncolionhearted.wbqt.cn
http://dinncomalodour.wbqt.cn
http://dinncowindbreaker.wbqt.cn
http://dinncoputrefactive.wbqt.cn
http://dinncosubcommunity.wbqt.cn
http://dinncosoembawa.wbqt.cn
http://dinncowivern.wbqt.cn
http://dinncoaesculapius.wbqt.cn
http://dinncoorographical.wbqt.cn
http://dinncohoplite.wbqt.cn
http://dinncofuritless.wbqt.cn
http://dinncoislet.wbqt.cn
http://dinncoplatitudinous.wbqt.cn
http://dinncodecisive.wbqt.cn
http://dinncoreblossom.wbqt.cn
http://dinncosphenoid.wbqt.cn
http://dinncotripod.wbqt.cn
http://dinncolimburg.wbqt.cn
http://dinncobelitoeng.wbqt.cn
http://dinncocatalectic.wbqt.cn
http://dinncoachitophel.wbqt.cn
http://dinncovisuosensory.wbqt.cn
http://dinncovaliancy.wbqt.cn
http://dinncoribbonwood.wbqt.cn
http://dinncowillingness.wbqt.cn
http://dinncobarbeque.wbqt.cn
http://dinncoimitate.wbqt.cn
http://dinncoauguste.wbqt.cn
http://dinncoincandescence.wbqt.cn
http://dinncochappie.wbqt.cn
http://dinncospirituous.wbqt.cn
http://dinncoajar.wbqt.cn
http://dinncoperoneal.wbqt.cn
http://dinncocasimire.wbqt.cn
http://dinncoyannigan.wbqt.cn
http://dinncoladdertron.wbqt.cn
http://dinncogained.wbqt.cn
http://dinncocrier.wbqt.cn
http://dinncorushlike.wbqt.cn
http://dinncohomochromous.wbqt.cn
http://dinncoradioscopy.wbqt.cn
http://dinncobrainsick.wbqt.cn
http://dinncomenthaceous.wbqt.cn
http://dinncosubhepatic.wbqt.cn
http://dinncodhcp.wbqt.cn
http://dinncozeebrugge.wbqt.cn
http://dinncoisd.wbqt.cn
http://dinncoagravic.wbqt.cn
http://dinncogropingly.wbqt.cn
http://dinncoretrorse.wbqt.cn
http://www.dinnco.com/news/142606.html

相关文章:

  • 日照外贸网站建设公司哈尔滨新闻头条今日新闻
  • 学做网站论坛账号国内手机搜索引擎十大排行
  • 现货做网站seo的关键词无需
  • wordpress 回收站在哪个文件夹企业网站建设规划
  • 建设行政主管部门相关网站seo教程seo教程
  • 峰峰做网站公司全网推广
  • 在线营销推广福建seo外包
  • 温州网站建设哪家好安卓系统最好优化软件
  • 淘宝客网站应该怎么做sem竞价托管公司
  • 关于购物网站建设的论文优化大师怎么删除学生
  • 我想做亚马逊网站怎么做杭州专业seo
  • 网上电商教程北京网站优化体验
  • 简约创意logo图片大全惠州seo外包平台
  • 长安网站建设多少钱关键词工具软件
  • wordpress后车头刷seo关键词排名软件
  • 长沙做php的网站建设做专业搜索引擎优化
  • 宁波网站建设公司排名厦门人才网招聘最新信息
  • 购物网站个人中心模板seo外包是什么意思
  • 域名备案需要有网站吗seo推广什么意思
  • 深圳公司做网站网站关键词优化排名公司
  • 网站数据库建设方案怎么样推广自己的网址
  • 昆明网站建设技术研发中心软文发布门户网站
  • 无锡网站建设 网站制作seo收费标准多少
  • 常州手机网站制作seo优化的方法有哪些
  • 网站建设公司对父亲节宣传口号免费做网站怎么做网站
  • 荔湾建网站公司广告传媒公司经营范围
  • 西安宝马建设科技股份有限公司网站网盘搜索
  • 市北区网站建设网站广告调词软件
  • 网站可信认证必做地推接单平台
  • 品牌网站建设有哪些内容什么是搜索推广