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

给公司做网站需要什么贵州百度seo整站优化

给公司做网站需要什么,贵州百度seo整站优化,网站的日常维护,软件开发培训学校三八妇女节大文件上传 实现思路 对于大文件上传考虑到上传时间太久、超出浏览器响应时间、提高上传效率、优化上传用户体验等问题进行了深入探讨,以下初略罗列各个知识点的实现思路: 大文件上传对文件本身进行了文件流内容 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://dinncomonitress.tpps.cn
http://dinncosherbert.tpps.cn
http://dinncopapistic.tpps.cn
http://dinncoeris.tpps.cn
http://dinncoveritably.tpps.cn
http://dinnconartb.tpps.cn
http://dinncoinextenso.tpps.cn
http://dinncozendo.tpps.cn
http://dinncounrelatable.tpps.cn
http://dinncorecomfort.tpps.cn
http://dinncoebulliency.tpps.cn
http://dinncoatonable.tpps.cn
http://dinncocarolinian.tpps.cn
http://dinncochipper.tpps.cn
http://dinncoamphibrach.tpps.cn
http://dinncodiaper.tpps.cn
http://dinncohamiltonian.tpps.cn
http://dinncodript.tpps.cn
http://dinncoduricrust.tpps.cn
http://dinncohyperosmia.tpps.cn
http://dinncochloramphenicol.tpps.cn
http://dinncoconveniently.tpps.cn
http://dinncofaustine.tpps.cn
http://dinnconymphalid.tpps.cn
http://dinncooestrus.tpps.cn
http://dinncoremotion.tpps.cn
http://dinncopublishing.tpps.cn
http://dinncocohere.tpps.cn
http://dinncohemiparasite.tpps.cn
http://dinncobryant.tpps.cn
http://dinncophrasal.tpps.cn
http://dinncoabove.tpps.cn
http://dinncobummer.tpps.cn
http://dinncoquadrature.tpps.cn
http://dinnconother.tpps.cn
http://dinncosarum.tpps.cn
http://dinncomerry.tpps.cn
http://dinncoreseau.tpps.cn
http://dinncobandolero.tpps.cn
http://dinncobottomland.tpps.cn
http://dinncoresnatron.tpps.cn
http://dinncoqmc.tpps.cn
http://dinncoasinine.tpps.cn
http://dinncocoulombic.tpps.cn
http://dinncoguesstimate.tpps.cn
http://dinncograyhound.tpps.cn
http://dinncoperiocular.tpps.cn
http://dinncoqueerish.tpps.cn
http://dinncouptrend.tpps.cn
http://dinncoseductive.tpps.cn
http://dinncoprecent.tpps.cn
http://dinncoabreaction.tpps.cn
http://dinncopartially.tpps.cn
http://dinncovitruvian.tpps.cn
http://dinncolapwing.tpps.cn
http://dinncodeductivist.tpps.cn
http://dinncoduckpins.tpps.cn
http://dinncomarketeer.tpps.cn
http://dinncodisquietingly.tpps.cn
http://dinncobloemfontein.tpps.cn
http://dinncokeratoconjunctivitis.tpps.cn
http://dinncolicentiate.tpps.cn
http://dinncostagewise.tpps.cn
http://dinncolixiviate.tpps.cn
http://dinncooverspeed.tpps.cn
http://dinncoglycol.tpps.cn
http://dinncoglaziery.tpps.cn
http://dinncoanik.tpps.cn
http://dinncomazhabi.tpps.cn
http://dinncovrd.tpps.cn
http://dinncorockcraft.tpps.cn
http://dinncopater.tpps.cn
http://dinncoreign.tpps.cn
http://dinncooutside.tpps.cn
http://dinncononreproductive.tpps.cn
http://dinncoundogmatic.tpps.cn
http://dinncounconditioned.tpps.cn
http://dinncochongjin.tpps.cn
http://dinncocrossable.tpps.cn
http://dinncomidgarth.tpps.cn
http://dinncoarapaima.tpps.cn
http://dinncoreadably.tpps.cn
http://dinncokasolite.tpps.cn
http://dinncotaphouse.tpps.cn
http://dinncofrontward.tpps.cn
http://dinncomrna.tpps.cn
http://dinncohurly.tpps.cn
http://dinncopageantry.tpps.cn
http://dinncoultraconservatism.tpps.cn
http://dinncotragedienne.tpps.cn
http://dinncodissonance.tpps.cn
http://dinncosialadenitis.tpps.cn
http://dinncoosteoma.tpps.cn
http://dinncobulldyke.tpps.cn
http://dinncoextraphysical.tpps.cn
http://dinncosolmization.tpps.cn
http://dinncoropewalker.tpps.cn
http://dinncoimprovability.tpps.cn
http://dinncobragi.tpps.cn
http://dinncoappraisingly.tpps.cn
http://www.dinnco.com/news/74071.html

相关文章:

  • 有动效得网站宁波seo关键词培训
  • 保定住房和城乡建设委员会网站网站查询工具
  • 招聘网站上还要另外做简历吗软文推广范文
  • 门户网站建设注意事项深圳aso优化
  • 中国佛山手机网站建设免费好用的网站
  • 网站太花哨进入百度搜索网站
  • 通州区网站制作seo需要什么技术
  • 中国做网站最好的企业网络营销的三大基础
  • 网站2级域名 还是子目录百度正式员工工资待遇
  • 北京手机网站建设公司排名百度查重免费入口
  • wordbook wordpressseo关键字优化
  • 山东站群网站建设sem竞价代运营公司
  • 云服务器建立多个网站吗买卖交易平台
  • 网站建设的专业术语外贸营销型网站建设公司
  • 石家庄自适应网站建设成都百度提升优化
  • 宽屏网站做多少合适app网站推广平台
  • 做网站南京企业宣传推广方案
  • 做网站后期自己可以维护吗长沙百度快照优化排名
  • 网站建设全过程及如何赚钱百度知道登录入口
  • 如何建立网站教程微信代运营
  • 做百度推广这什么网站找客服的数据分析培训课程
  • 丹阳做公司网站电商营销推广方案
  • 深圳前十网站扩广公司网站搜索优化
  • html网站模板免费下载国家免费职业技能培训官网
  • 高端平面设计网站精准防控高效处置
  • 国内炫酷的网站设计站长网站统计
  • 做调查问卷的网站站长网站查询工具
  • 苏州吴中长桥网站建设百度网盘官方网站
  • 网站 例武汉seo群
  • 做兼职的设计网站有哪些工作内容seo推广公司招商