D365: 压缩和解压附件
D365中,如果接口中需要传送文件,我们可以将文件转换为字节流,以字节流的方式传递,如果接口需要一次传送多个附件,我们可以将多个文件先转换为压缩包字节流进行传递,三方系统解析压缩包字节流,拿到所有的文件
代码实现
1,附件转字节流
//DocumentManagement::getAttachmentStream(docuRef); static public System.Byte[] streamToBytes(System.IO.Stream _stream) { System.Byte[] bytes; System.Text.Encoding encoding = new System.Text.UTF8Encoding(false); using(System.IO.BinaryReader br = new System.IO.BinaryReader(_stream, encoding)) { bytes = br.readBytes(_stream.Length); } return bytes; }
2,将字节流转换为zip压缩包流
List listFielbit = new List(Types::Container); container con; stream = DocumentManagement::getAttachmentStream(docuRef); System.Byte[] bytes = streamToBytes(stream); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(bytes); con = [memoryStream.ToArray()]; listFielbit.addEnd(con);
System.IO.MemoryStream zipArchiveStream = new System.IO.MemoryStream();
using (ZipArchive zipArchive = new ZipArchive(zipArchiveStream, ZipArchiveMode::Create, true))
{
ListEnumerator filesEnumerator = listFielbit.getEnumerator();
while(filesEnumerator.moveNext())
{
ZipArchiveEntry dataFileEntry = zipArchive.CreateEntry(docuValue.Name + '.' + docuValue.FileType);
using (System.IO.Stream dataFileEntryStream = dataFileEntry.Open())
{
System.Byte[] fileBytes;
fileBytes = conPeek(filesEnumerator.current(), 1);
dataFileEntryStream.Write(fileBytes, 0, fileBytes.Length);
}
}
}
3, 解压压缩包字节流,转换为单个文件字节流
Stream streamZip = new MemoryStream(bmpEntity.Fielbit); using (ZipArchive zipArchive = new ZipArchive(streamZip)) { foreach (ZipArchiveEntry entry in zipArchive.Entries) { Stream streamFile = entry.Open(); byte[] fileBytes; using (var memoryStream = new MemoryStream()) { streamFile.CopyTo(memoryStream); fileBytes = memoryStream.ToArray(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
2021-03-02 D365 覆写标准逻辑