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

wordpress 工作室湖南靠谱seo优化报价

wordpress 工作室,湖南靠谱seo优化报价,做网站做注册登录的难点,wordpress内容页标题目录 1、文件下载的原理2、具体实现2.1 提前准备2.2 服务器端的实现2.3 请求端的实现 3、代码下载4、更多特性4.1 单独压缩文件4.2 解析4.2.1 整体解析4.2.2 单个文件解析 4.3 其他4.3.1 设置压缩级别4.3.2 密码保护4.3.3 进度反馈 5、参考资料 1、文件下载的原理 在实际应用环…

目录

  • 1、文件下载的原理
  • 2、具体实现
    • 2.1 提前准备
    • 2.2 服务器端的实现
    • 2.3 请求端的实现
  • 3、代码下载
  • 4、更多特性
    • 4.1 单独压缩文件
    • 4.2 解析
      • 4.2.1 整体解析
      • 4.2.2 单个文件解析
    • 4.3 其他
      • 4.3.1 设置压缩级别
      • 4.3.2 密码保护
      • 4.3.3 进度反馈
  • 5、参考资料

1、文件下载的原理

在实际应用环境中,文件的下载是一个特别常见的需求。为了传输文件的大小,往往需要对文件进行压缩,因此就涉及到文件的压缩及文件的下载,具体步骤有5步。(1)、请求端发出文件的请求。这个可以根据实际需求,使用get/post及参数,发起一个请求。(2)、服务器收到请求后,会根据需求生成(或者准备)相关文件(最常用的例子就是:患者通过医院App下载处方、门诊记录单、诊断证明书之类)【这一步往往是偏向业务相关】。(3)然后将这些生成的文件,压缩成zip文件格式。(4)、服务端返回数据。(5)、客户端收到数据后,解析Response中的Stream,并使用FileStream将文件存储到本地。
具体的原理如下图所示
文件下载的原理

2、具体实现

2.1 提前准备

我们提前准备了文件(这步主要是模拟实际业务中生成的文件夹及文件)。在fhirs文件夹下有一个index.xml文件和emrfhirs文件夹,并且在emrfhirs文件夹内有001fhir.json002fhir.json003fhir.json三个文件。文档的目录结构如下:
在这里插入图片描述
在这里插入图片描述

2.2 服务器端的实现

创建webapi项目,实现下载文件的方法。具体代码如下

[HttpPost]
public async Task<ActionResult> getCompressionFile()
{try{/** 接收客户端的参数,本示例为了简化,不进行处理IFormCollection form = HttpContext.Request.Form;string? username = form["UserName"];string? password = form["Password"];string? remoteFilePath = form["RemotePath"];**/string source_folder = @"D:\data\fhirs"; //需要压缩的文件的路径,就是刚才上面提到的文件夹及文件string destination_path = @"D:\data\fhirsemr.zip";  //要将上面的文件夹变成的压缩文件// using System.IO.Compression;//将文件进行压缩await Task.Run(() =>{ZipFile.CreateFromDirectory(source_folder, destination_path);});//这个地方涉及一个知识//zip压缩文件,它的content-type是application/x-zip-compressed//rar压缩文件,它的content-type是application/octet-streamvar stream = new FileStream(destination_path, FileMode.Open, FileAccess.Read);return new FileStreamResult(stream, "application/x-zip-compressed"){FileDownloadName = "ermfile"};}catch (Exception ex){throw new Exception(ex.Message);}
}

上面就是服务端的代码

2.3 请求端的实现

客户端的请求端实现如下

//url:服务端的地址
//localFileFullPath:将文件存储到本地的路径
//otherParams:给业务使用的参数
private string GetFile(string url, string localFileFullPath, string otherParams)
{try{Uri uri = new Uri(url);HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);request.Method = "post"; //post、getrequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";request.KeepAlive = true;request.Credentials = CredentialCache.DefaultCredentials;using (Stream requestStream = request.GetRequestStream()){byte[] formDataBytes = Encoding.UTF8.GetBytes(otherParams);requestStream.Write(formDataBytes, 0, formDataBytes.Length);}HttpWebResponse response = (HttpWebResponse)request.GetResponse();//判断路径,并创建路径string path = Path.GetDirectoryName(localFileFullPath);if (!Directory.Exists(path) && !string.IsNullOrEmpty(path)){Directory.CreateDirectory(path);}//将文件using (FileStream fs = new FileStream(localFileFullPath, FileMode.Create, FileAccess.Write, FileShare.None)){using (Stream responseStream = response.GetResponseStream()){//创建本地文件写入流byte[] bArr = new byte[1024];int iTotalSize = 0;int size = responseStream.Read(bArr, 0, bArr.Length);while (size > 0){iTotalSize += size;fs.Write(bArr, 0, size);size = responseStream.Read(bArr, 0, bArr.Length);}}}return "Success-获取文件成功";}catch (Exception ex){return "Error-"+ex.Message;}
}

3、代码下载

代码比较简单,无代码

4、更多特性

4.1 单独压缩文件

如果你只想压缩特定的文件,而不是整个文件夹,你可以使用FileStream和ZipArchive来实现。实际上就是NET中的文件操作

using System.IO;
using System.IO.Compression;string startPath = @"c:\example\file.txt"; // 要压缩的文件路径
string zipPath = @"c:\example\result.zip"; // 压缩后的ZIP文件路径using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Create))
{using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create)){ZipArchiveEntry readmeEntry = archive.CreateEntry("file.txt");using (StreamWriter writer = new StreamWriter(readmeEntry.Open())){using (StreamReader reader = new StreamReader(startPath)){string content = reader.ReadToEnd();writer.Write(content);}}}
}

4.2 解析

4.2.1 整体解析

解压文件同样简单。我们可以使用ZipFile类的ExtractToDirectory方法来解压ZIP文件到指定文件夹:

using System.IO.Compression;string zipPath = @"c:\example\start.zip"; // 要解压的ZIP文件路径
string extractPath = @"c:\example\extract"; // 解压后的文件夹路径ZipFile.ExtractToDirectory(zipPath, extractPath);

这段代码会将start.zip文件解压到extract文件夹中。

4.2.2 单个文件解析

如果你需要更细粒度的控制,比如只解压ZIP文件中的特定文件,你可以使用ZipArchive来实现:

using System.IO;
using System.IO.Compression;string zipPath = @"c:\example\start.zip"; // 要解压的ZIP文件路径
string extractPath = @"c:\example\extract"; // 解压后的文件夹路径
string fileToExtract = "file.txt"; // 要解压的文件名using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{ZipArchiveEntry entry = archive.GetEntry(fileToExtract);if (entry != null){string fullPath = Path.Combine(extractPath, entry.FullName);entry.ExtractToFile(fullPath, overwrite: true);}
}

这段代码会从start.zip文件中解压出file.txt文件到extract文件夹中

4.3 其他

4.3.1 设置压缩级别

在压缩文件时,你可以指定压缩级别来优化压缩效果。ZipArchiveMode.Create方法接受一个CompressionLevel枚举参数,允许你选择不同的压缩级别:

using (FileStream zipToOpen = new FileStream(zipPath, FileMode.Create))
{using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Create, true)){// ... 添加文件到ZIP归档中}
}

在这个例子中,第三个参数是布尔值,表示是否压缩归档中的条目。你也可以通过ZipArchiveEntry.CompressionLevel属性为单个条目设置压缩级别。

4.3.2 密码保护

.NETSystem.IO.Compression命名空间不支持为ZIP文件添加密码保护。如果你需要这个功能,你可能需要使用第三方库,如DotNetZip

4.3.3 进度反馈

在压缩或解压大文件时,提供进度反馈是一个很好的用户体验。然而,System.IO.Compression命名空间并没有直接提供进度反馈的机制。你可以通过计算处理的文件大小与总大小来估算进度,并使用例如IProgress<T>接口来报告进度。

5、参考资料

这篇文章主要参考了这篇文章:NET环境下使用原生方法实现文件压缩与解压


文章转载自:
http://dinncojaileress.zfyr.cn
http://dinncotempestuous.zfyr.cn
http://dinncopostliminium.zfyr.cn
http://dinncoseductively.zfyr.cn
http://dinncocongressite.zfyr.cn
http://dinncoimpeditive.zfyr.cn
http://dinncomozarab.zfyr.cn
http://dinncoailing.zfyr.cn
http://dinncoseram.zfyr.cn
http://dinncolst.zfyr.cn
http://dinncohumorously.zfyr.cn
http://dinncoretroactive.zfyr.cn
http://dinncolionship.zfyr.cn
http://dinncoprolongation.zfyr.cn
http://dinncomarginal.zfyr.cn
http://dinncospermous.zfyr.cn
http://dinncotribal.zfyr.cn
http://dinncoleister.zfyr.cn
http://dinncotrilobate.zfyr.cn
http://dinncosemiserious.zfyr.cn
http://dinncothick.zfyr.cn
http://dinncogranulation.zfyr.cn
http://dinncoundependable.zfyr.cn
http://dinncopecker.zfyr.cn
http://dinncoproscriptive.zfyr.cn
http://dinncospringtide.zfyr.cn
http://dinncosaucepot.zfyr.cn
http://dinncowestmark.zfyr.cn
http://dinncorq.zfyr.cn
http://dinncocolombia.zfyr.cn
http://dinncoautointoxication.zfyr.cn
http://dinncoundercroft.zfyr.cn
http://dinncocaprification.zfyr.cn
http://dinncojasmine.zfyr.cn
http://dinncoauscultator.zfyr.cn
http://dinnconazirite.zfyr.cn
http://dinncoseptisyllable.zfyr.cn
http://dinncoplaya.zfyr.cn
http://dinncodamningness.zfyr.cn
http://dinncoexplainable.zfyr.cn
http://dinncoexperimentalize.zfyr.cn
http://dinncodyeing.zfyr.cn
http://dinncopsychohistorical.zfyr.cn
http://dinncodasyure.zfyr.cn
http://dinncolithotritor.zfyr.cn
http://dinncoradiosurgery.zfyr.cn
http://dinncoexcitron.zfyr.cn
http://dinncoimperishably.zfyr.cn
http://dinnconapkin.zfyr.cn
http://dinncoanta.zfyr.cn
http://dinncosubmetallic.zfyr.cn
http://dinncocredited.zfyr.cn
http://dinncofootpad.zfyr.cn
http://dinncohydromechanical.zfyr.cn
http://dinncopaintbrush.zfyr.cn
http://dinncopettifogging.zfyr.cn
http://dinncoflair.zfyr.cn
http://dinncohumbly.zfyr.cn
http://dinncomelaleuca.zfyr.cn
http://dinncoundertake.zfyr.cn
http://dinncodearness.zfyr.cn
http://dinncomizz.zfyr.cn
http://dinncoskeletonless.zfyr.cn
http://dinncoacquirable.zfyr.cn
http://dinncomyeloblast.zfyr.cn
http://dinncofaroese.zfyr.cn
http://dinncomapi.zfyr.cn
http://dinncofoodgrain.zfyr.cn
http://dinncodelustre.zfyr.cn
http://dinncoearthshine.zfyr.cn
http://dinncocor.zfyr.cn
http://dinncovirtually.zfyr.cn
http://dinncowardenry.zfyr.cn
http://dinncounlessened.zfyr.cn
http://dinncopartwork.zfyr.cn
http://dinncolz.zfyr.cn
http://dinncoshorthanded.zfyr.cn
http://dinncoduykerbok.zfyr.cn
http://dinncoabolishable.zfyr.cn
http://dinncogam.zfyr.cn
http://dinncolineskipper.zfyr.cn
http://dinncocomedietta.zfyr.cn
http://dinncorigidize.zfyr.cn
http://dinncopeephole.zfyr.cn
http://dinncoparure.zfyr.cn
http://dinncohijacker.zfyr.cn
http://dinncoappoint.zfyr.cn
http://dinncofevered.zfyr.cn
http://dinncotransudation.zfyr.cn
http://dinncoirdp.zfyr.cn
http://dinncograveyard.zfyr.cn
http://dinncobeleague.zfyr.cn
http://dinncobackbreaker.zfyr.cn
http://dinncopleopod.zfyr.cn
http://dinncomesomerism.zfyr.cn
http://dinncoreminiscent.zfyr.cn
http://dinncoresolvability.zfyr.cn
http://dinncopipal.zfyr.cn
http://dinncoencoder.zfyr.cn
http://dinncopostmeridian.zfyr.cn
http://www.dinnco.com/news/3351.html

相关文章:

  • 旅游网站网页设计模板代码北京网站推广公司
  • 网页设计代码模板网站java成品网站
  • 扁平化设计风格的网站模板免费下载合川网站建设
  • 做网站直接从网上的icon吗宁波seo
  • jquery效果网站网站关键词怎么添加
  • 网站搭建哪里找更靠谱网站推广seo优化
  • 江苏网站设计深圳 网站制作
  • 网站怎么做的qq邮件订阅可以推广网站
  • 电子商务专业就业方向及就业前景企业网站seo排名优化
  • 做网站都要掌握什么上海seo关键词优化
  • 导航网站能个人备案新闻发布会
  • 赣州建设监督网站五八精准恶意点击软件
  • 杭州做网站软件北京口碑最好的教育机构
  • php商城网站建设搜索引擎推广有哪些平台
  • 北京制作网站的公司简介点击器
  • 信息产业部网站备案查询单页网站设计
  • 四川内江网站建设seo技术教程网
  • 资阳公司网站建设大概需要多少钱
  • 政府网站做的不好去哪里投诉世界比分榜
  • wordpress 中文文档下载seo整站优化报价
  • 行业网站建设费用明细疫情最新情况 最新消息 全国
  • DW自动生成代码做网站中国十大搜索引擎排名最新
  • 做网站分流湖南关键词优化快速
  • 关于网页设计毕业论文优化培训课程
  • 莆田做网站的公司百度seo快速排名优化
  • jsp网站建设技术案例现在百度怎么优化排名
  • 英语培训网站源码山东免费网络推广工具
  • 贵阳市小程序网站开发公司引流推广方案
  • 网站空间 哪个速度快提高关键词排名的软文案例
  • 在哪个网站可以自助建站故事式软文范例500字