• 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();
          }
       }
    }
  • 相关阅读:
    java-判断某一年是否是闰年
    java-不用其他变量实现两变量互换
    java基础一标识符,运算符
    robotium学习(三)
    robotium学习(二)
    robotium学习(一)
    AX2012 订单折扣的应用
    AX2012 用户收藏夹复制
    ueditor 改变图片上传路径
    if else 多路分支,多条件运算
  • 原文地址:https://www.cnblogs.com/dingkui/p/15957563.html
Copyright © 2020-2023  润新知