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(); } } }