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

椒江住房和城乡建设部网站网络营销的方法包括哪些

椒江住房和城乡建设部网站,网络营销的方法包括哪些,做兼职上什么网站找,做网站学习什么文件上传 文件上传的方案: 大文件上传:将大文件切分成较小的片段(通常称为分片或块),然后逐个上传这些分片。这种方法可以提高上传的稳定性,因为如果某个分片上传失败,只需要重新上传该分片而…

文件上传

文件上传的方案:

  1. 大文件上传:将大文件切分成较小的片段(通常称为分片或块),然后逐个上传这些分片。这种方法可以提高上传的稳定性,因为如果某个分片上传失败,只需要重新上传该分片而不需要重新上传整个文件。同时,分片上传还可以利用多个网络连接并行上传多个分片,提高上传速度
  2. 断点续传:在上传过程中,如果网络中断或上传被中止,断点续传技术可以记录已成功上传的分片信息,以便在恢复上传时继续上传未完成的部分,而不需要重新上传整个文件。这种技术可以大大减少上传失败的影响,并节省时间和带宽。
前端实现
<head><meta charset="UTF-8"><title>Title</title><style>input {background-color: #f5f5f5;border: 1px solid #ccc;border-radius: 5px;cursor: pointer;outline: none;font-size: 14px;color: #333;text-align: center;line-height: 30px;font-size: 40px;}</style>
</head>
<body><!--  上传文件  --><input type="file" id="file" name="file" value="上传文件" />
</body>

第一步:获取元素,监听change事件。获取到文件的信息之后,利用file原型上面的 blob对象的slice方法来进行分割

// 获取文件,监听有无上传const file = document.getElementById('file');file.addEventListener('change', function (e) {// 获取文件信息const file = e.target.files[0];const chunks = sliceFile(file);uploadFile(chunks)})// 分片function sliceFile(file, chunkSize = 1024 * 1024 * 3) {let chunks = []for (let i = 0; i < file.size; i+= chunkSize) {chunks.push(file.slice(i, i + chunkSize))}return chunks}

第二步:将这些分片的文件片,编入编号和文件名后以formData的格式上传,并且将结果放入promise.all这个方法中,如果全部成功的化,那么就调用合并函数,将这个视频进行合并

// 上传function uploadFile(chunks) {let list = []for (let i = 0; i < chunks.length; i++) {let formData = new FormData();formData.append('index', i)formData.append('name', "wenjian")formData.append('file', chunks[i])list.push(fetch("http://localhost:8080/upload", {method: 'POST',body: formData}))}// 监听事件是否成功Promise.all(list).then(res => {// 发送合并请求fetch("http://localhost:8080/merge", {method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify({name: "ceshi.gif",})}).then(res => {console.log(res)}).catch(e => {console.log(e)})}).catch(e=> {console.log(e)})}
nodejs端实现

安装依赖

  1. express 帮我们启动服务,并且提供接口
  2. multer 读取文件,存储
  3. cors 解决跨域

初始化 multer.diskStorage

  • destination 存储的目录
  • filename 存储的文件名(我是通过index-文件名存储的你也可以改)
// 1. 初始化multer
const storage = multer.diskStorage({destination:function (req,file,cb) {cb(null,'./upload')},filename:function (req,file,cb) {cb(null,`${req.body.index}-${req.body.name}`)}
})

放到接口上面,就可以将分完片的文件上传

// 2. 配置multer
const upload = multer({storage:storage})
// 3. 创建上传接口
app.post('/upload',upload.single('file'),(req,res) => {res.send('上传成功')
})

合并文件:先读取分片文件的文件名,然后把这些文件重新的进行排序,合成一个新的文件

完整代码:

import express from 'express'
import multer from 'multer'
import cors from 'cors'
import fs from 'node:fs'
import path from 'node:path'// 1. 初始化multer
const storage = multer.diskStorage({destination:function (req,file,cb) {cb(null,'./upload')},filename:function (req,file,cb) {cb(null,`${req.body.index}-${req.body.name}`)}
})
const app = express()
app.use(cors())
app.use(express.json())
// 2. 配置multer
const upload = multer({storage:storage})
// 3. 创建上传接口
app.post('/upload',upload.single('file'),(req,res) => {res.send('上传成功')
})
// 4. 合并文件
app.post("/merge",(req,res) => {if(!req.body.name) return res.send('文件名不能为空')let uploadDir = "./upload"// 读取分片文件let files = fs.readdirSync(path.join(process.cwd(), uploadDir))// 重新排序files = files.sort((a,b) => a.split('-')[0] - b.split('-')[0])// 合并文件let writeDir = path.join(process.cwd(),"./video",`${req.body.name}`)files.forEach(item => {fs.appendFileSync(writeDir,fs.readFileSync(path.join(process.cwd(),uploadDir,item)))fs.unlinkSync(path.join(process.cwd(),uploadDir,item))})res.send('合并成功')
})
app.listen(8080,() => console.log('Server is running on port 8080'))

文件流下载

文件流下载是一种通过将文件内容以流的形式发送给客户端,实现文件下载的方法。它适用于处理大型文件或需要实时生成文件内容的情况。

nodejs端实现

响应头

  1. Content-Type 指定下载文件的 MIME 类型
    • application/octet-stream(二进制流数据)
    • application/pdf:Adobe PDF 文件。
    • application/json:JSON 数据文件
    • image/jpeg:JPEG 图像文件
  2. Content-Disposition 指定服务器返回的内容在浏览器中的处理方式。它可以用于控制文件下载、内联显示或其他处理方式
    • attachment:指示浏览器将响应内容作为附件下载。通常与 filename 参数一起使用,用于指定下载文件的名称
    • inline:指示浏览器直接在浏览器窗口中打开响应内容,如果内容是可识别的文件类型(例如图片或 PDF),则在浏览器中内联显

代码实现:

import express from 'express'
import fs from 'fs'
import path from 'path'
import cors from 'cors'const app = express()
app.use(cors())
app.use(express.json())
app.use(express.static(path.join(process.cwd(),"static")))app.post("/upload", (req, res) => {let fileName = req.body.fileNameif(!fileName) return res.send({message: "File name is required"})let filePath = path.join(process.cwd(),"static", fileName)let readStream = fs.readFileSync(filePath)// 设置响应头res.setHeader('Content-Type', 'application/octet-stream')res.setHeader('Content-Disposition', 'attachment;filename=' + fileName)res.send(readStream)
})app.listen(3000,() => console.log('Server is running on port 3000'))

前端逻辑

前端核心逻辑就是接受的返回值是流的方式arrayBuffer,转成blob,生成下载链接,模拟a标签点击下载

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><style>input {background-color: #f5f5f5;border: 1px solid #ccc;border-radius: 5px;cursor: pointer;outline: none;font-size: 14px;color: #333;text-align: center;line-height: 30px;font-size: 40px;}</style>
</head>
<body><!--  上传文件  --><input type="button"  value="下载文件" />
</body>
<script>let btn = document.querySelector('input');btn.onclick = function () {fetch("http://localhost:3000/upload",{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({fileName:'1.png',})}).then(res => res.arrayBuffer()).then(res => {// 1. 转为bloblet blob = new Blob([res],{type:'image/png'})// 2. 创建a标签let a = document.createElement('a')// 4. 设置a标签的href属性为blob地址a.href = URL.createObjectURL(blob)// 3. 设置下载文件名a.download = '1.png'// 5. 模拟点击a标签a.click()// 6. 移除a标签a.remove()})}
</script>
</html>

文章转载自:
http://dinncosynthesize.tpps.cn
http://dinncoineffectively.tpps.cn
http://dinncozomba.tpps.cn
http://dinncomisoneism.tpps.cn
http://dinncounperson.tpps.cn
http://dinncoakathisia.tpps.cn
http://dinncointerlibrary.tpps.cn
http://dinncodilutor.tpps.cn
http://dinncomillidegree.tpps.cn
http://dinncoprofane.tpps.cn
http://dinncofpm.tpps.cn
http://dinncopshaw.tpps.cn
http://dinncosateen.tpps.cn
http://dinncocrucial.tpps.cn
http://dinncosbr.tpps.cn
http://dinncodhahran.tpps.cn
http://dinncomarketstead.tpps.cn
http://dinncounhealthiness.tpps.cn
http://dinncopredormition.tpps.cn
http://dinncogoon.tpps.cn
http://dinncotzaristic.tpps.cn
http://dinncobromate.tpps.cn
http://dinncolandsturm.tpps.cn
http://dinncoundergraduate.tpps.cn
http://dinncosycamore.tpps.cn
http://dinncolissotrichous.tpps.cn
http://dinncoagglomerant.tpps.cn
http://dinncobordel.tpps.cn
http://dinncovalidity.tpps.cn
http://dinncocancroid.tpps.cn
http://dinncovodka.tpps.cn
http://dinncoconsternate.tpps.cn
http://dinncoconsider.tpps.cn
http://dinncopenal.tpps.cn
http://dinncopira.tpps.cn
http://dinncopigeontail.tpps.cn
http://dinncofluffhead.tpps.cn
http://dinncofussbudget.tpps.cn
http://dinncoscreenwiper.tpps.cn
http://dinncocontradistinguish.tpps.cn
http://dinncocomatose.tpps.cn
http://dinncoscantly.tpps.cn
http://dinncorsn.tpps.cn
http://dinncoconcubinage.tpps.cn
http://dinncoproscript.tpps.cn
http://dinncoacross.tpps.cn
http://dinncolaughter.tpps.cn
http://dinncofoison.tpps.cn
http://dinncorequin.tpps.cn
http://dinncoriposte.tpps.cn
http://dinncoalveoli.tpps.cn
http://dinncoreclaim.tpps.cn
http://dinncodeanship.tpps.cn
http://dinncofasciole.tpps.cn
http://dinncolactic.tpps.cn
http://dinncoconstancy.tpps.cn
http://dinncomonthly.tpps.cn
http://dinncoquadriga.tpps.cn
http://dinncocataphoric.tpps.cn
http://dinncotitograd.tpps.cn
http://dinncopapyrus.tpps.cn
http://dinncounstalked.tpps.cn
http://dinncoeclectically.tpps.cn
http://dinncohypnophobia.tpps.cn
http://dinncoartifact.tpps.cn
http://dinncopikestaff.tpps.cn
http://dinncoachromate.tpps.cn
http://dinncoautoecism.tpps.cn
http://dinncobackwood.tpps.cn
http://dinncopiezocrystal.tpps.cn
http://dinncodrying.tpps.cn
http://dinncocounsel.tpps.cn
http://dinncoscaup.tpps.cn
http://dinncogenal.tpps.cn
http://dinncoruthless.tpps.cn
http://dinncosyllabography.tpps.cn
http://dinncomicrohardness.tpps.cn
http://dinncoandromonoecious.tpps.cn
http://dinncofocus.tpps.cn
http://dinncoextractive.tpps.cn
http://dinncocircumcise.tpps.cn
http://dinncomonarchist.tpps.cn
http://dinncoalgidity.tpps.cn
http://dinncoconfectioner.tpps.cn
http://dinncoviscidity.tpps.cn
http://dinncobeneficence.tpps.cn
http://dinncooverfeeding.tpps.cn
http://dinncofloozie.tpps.cn
http://dinncorosepoint.tpps.cn
http://dinncoumiak.tpps.cn
http://dinncogotter.tpps.cn
http://dinncobeeves.tpps.cn
http://dinncokalahari.tpps.cn
http://dinncosatinize.tpps.cn
http://dinncojoviologist.tpps.cn
http://dinncomicrodontism.tpps.cn
http://dinncotemplate.tpps.cn
http://dinncoaerobe.tpps.cn
http://dinncoxenogamy.tpps.cn
http://dinncowolfram.tpps.cn
http://www.dinnco.com/news/155315.html

相关文章:

  • 免费html模板素材网站网推是什么意思
  • 做艺人资料卡的网站互联网运营推广
  • 泰州网站制作工具交换链接平台
  • 快速建设网站服务百度的广告怎么免费发布
  • 收藏夹网站的图标怎么做的网站怎么做谷歌推广
  • 重庆营销网站专业的seo排名优化
  • 紫色网站seo技巧分享
  • axure开始怎么做网站首页开鲁网站seo
  • 如何做区块链网站杭州seo工作室
  • 做网站服务器需要系统网络营销中的seo与sem
  • iis做网站之vps河南郑州最新消息
  • 简易app2023网站seo
  • 抖音运营推广成都搜索优化排名公司
  • qt设计精美ui充电宝关键词优化
  • 廊坊快速排名优化网站优化 福州
  • 做网站如何选择颜色最新seo操作
  • 企业邮箱注册申请163免费西安seo服务公司排名
  • 中关村在线官方网站常州谷歌优化
  • 商贸公司起名大全最新东莞seo广告宣传
  • 做移动网站优化简述企业网站推广的一般策略
  • 金桥网站建设站长之家点击进入
  • 网站建设介绍百度竞价代理商
  • 漳州哪里做网站北京企业网络推广外包
  • 广东智能网站建设配件公司信息发布推广平台
  • 网站建设的颜色值百度seo是啥意思
  • php网站数据库怎样导入sem竞价是什么
  • 做富集分析的网站企业网站设计价格
  • 武冈企业建站网络优化的工作内容
  • 如何做色情网站网站整站优化
  • 赣州市住房和城乡建设局网站抖音推广平台联系方式