【问题标题】:retrieving Binary/Blob files from Microsoft Dynamics Nav with ASP.NET使用 ASP.NET 从 Microsoft Dynamics Nav 检索二进制/Blob 文件
【发布时间】:2014-11-11 14:52:14
【问题描述】:

我正在使用具有文件附件表的 MS Dynamics 导航数据库。这些文件存储在 MS SQL 中。我可以使用我构建的自定义 asp.net 应用程序将文件拉到我的桌面,但是当我打开文件时,它们已损坏。这些是位于数据库“图像”文件类型列中的 PDF 文件,我已尝试下载 20 多个文件。它们的大小都不同,似乎下载成功。
我怀疑这些是 PDF 文件的原因是因为二进制列旁边的列给了我 PDF 格式的文件名。在我下载到不同的图像格式后,我也尝试重命名文件,但是当我尝试打开它时没有任何运气。这不是我第一个从 MS SQL 数据库检索二进制文件的项目。如果有人以前从 Nav 数据库中获取文件,请帮助我。当我在浏览器中给它一个特定的 ID 时,我编写了下面的示例代码来使用 LINQ to SQL 检索文件。如果您知道二进制文件本身中的任何类型的压缩或加密以及如何成功获取文件以读取它,请告诉我。谢谢

 protected void getFileFromID(string queryid)
    {
        string Filename = string.Empty;


        byte[] bytes;

        try
        {
            DataClassesFilesDataContext dcontext = new DataClassesFilesDataContext();

            var myfile = (from file in dcontext.Comptroller_File_Attachments
                          where file.No_ == queryid
                          select file).First();

            if (myfile.Table_ID.ToString().Length > 0 && myfile.Attachment != null)
            {
                Filename = myfile.FileName.ToString();

                bytes = myfile.Attachment.ToArray();
                Response.Clear();                   
                Response.ContentType = "application/octet-stream";                 
                Response.AddHeader("Content-Disposition", "attachment; filename=" + Filename);
                Response.BinaryWrite(bytes);               
                Response.End();
            }

            else
            {
                Response.Write("no file exist");
            }
        }

        catch (Exception e)
        {
            Response.Write(e);

        }
    }

【问题讨论】:

    标签: asp.net sql-server linq-to-sql navision


    【解决方案1】:

    嗯。我想到了。我在博客上读到 4 个字节是摆脱的“幻数”。所以你所要做的就是从 BLOB 字节数组中去掉 4 个字节,然后用 DeflateStream 解压缩它。我在下面发布的示例代码是一个示例,它接收一个字节数组并使用 LINQ-to-SQL 跳过前 4 个并返回第二个函数的字节和字符串文件名。它还传入一个 queryid 字符串参数。我相信为了提高效率,代码可以得到更多改进。对于那些对此有问题的人,请尝试一下。

        //get bytes and remove first 4 bytes from bytes array
      protected Tuple<byte[], string> getBytesfromFile(string queryID)
      {
    
          byte[] MyFilebytes = null;
          string filename = string.Empty;
    
            try
            {
                DataClassesFilesDataContext dcontext = new DataClassesFilesDataContext();
    
                var myfile = (from file in dcontext.Comptroller_File_Attachments
                              where file.No_ == queryID
                              select file).First();
    
                if (myfile.Table_ID.ToString().Length > 0 && myfile.Attachment != null)
                {
    
                    MyFilebytes = myfile.Attachment.ToArray().Skip(4).ToArray();
                   filename = myfile.FileName.ToString();
                }
    
                else
                    Response.Write("no byte to return");
    
            }
          catch 
         {
             Response.Write("no byte");
         }
    
            return Tuple.Create(MyFilebytes, filename);
      }
    
        //after getting the remaining bytes (after removing 4 first byte) deflate the byte and then store it in a memory steam and get the result back.
      protected void getFile()
      {
          try
          {
              string Filename = string.Empty;
              byte[] myfile = getBytesfromFile(getQueryID()).Item1;
    
              byte[] result;
    
              using (Stream input = new DeflateStream(new MemoryStream(myfile),
                                            CompressionMode.Decompress))
              {
                  using (MemoryStream output = new MemoryStream())
                  {
                      input.CopyTo(output);
                      result = output.ToArray();
                  }
              }
    
              Filename = getBytesfromFile(getQueryID()).Item2;
    
    
              Response.Clear();
              Response.ContentType = "application/octet-stream";
              Response.AddHeader("Content-Disposition", "attachment; filename=" + Filename);
              Response.BinaryWrite(result);
              Response.End();
          }
    
          catch (Exception e)
          {
              Response.Write(e);
          }
    
      }
        //pass in file id
        protected string getQueryID()
        {
    
            QueryID.QueryStringID = Request.QueryString["fileid"];
            return QueryID.QueryStringID;
        }
    

    【讨论】:

    • 完美的答案,今天甚至为 NAV 2018 工作!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-05
    • 2017-06-08
    相关资源
    最近更新 更多