【问题标题】:Is the C# library MPXJ able to read in a file from a MemoryStream?C# 库 MPXJ 是否能够从 MemoryStream 读取文件?
【发布时间】:2016-04-15 17:53:20
【问题描述】:

我正在使用the library MPXJ,效果很好,但我现在希望用户能够上传自己的文件(asp.net-mvc 站点),它以 HttpPostedFileBase 的形式出现在服务器端表单中,然后我转换到内存流使用:

    var stream = new MemoryStream();
    httpPostedFile.InputStream.CopyTo(stream);

鉴于此,我试图弄清楚如何将其作为 MemoryStream 读取(相对于磁盘上的文件位置)

现在我有这样的东西:

    public ProjectFile Import(string filePathandName)
    {
        MPPReader reader = new MPPReader();
        ProjectFile project = reader.read(filePathandName);

我想要这样的东西:

    public ProjectFile Import(MemoryStream stream)
    {
        MPPReader reader = new MPPReader();
        ProjectFile project = reader.read(stream);

这可能是“本机”还是我需要将文件保存在我的服务器上然后从那里读取(试图避免该选项)?

【问题讨论】:

    标签: c# memorystream mpxj


    【解决方案1】:

    MPPReader.Read() 方法只接受 4 种类型的参数,其中没有一种是 MemoryStream,除了一种似乎是在库本身中定义的类型:

    • java.io.File
    • java.io.InputStream
    • org.apache.poi.poifs.filesystem.POIFSFileSystem
    • 字符串

    您当前正在使用 string 参数,因为它需要一个路径,但是您可能得到的最接近的方法似乎是尝试将现有的 MemoryStream 对象复制到库中找到的 InputStream 类型和使用它(如果存在这种类型的支持)。

    【讨论】:

      【解决方案2】:

      MPXJ 附带一对名为 DotNetInputStreamDotNetOutputStream 的类,它们充当 .Net 流的包装器,因此它们可以在 MPXJ 期望 Java InputStreamOutputStream 的地方使用。

      这是来自DotNetInputStream的相关评论:

      /// <summary>
      /// Implements a wrapper around a .Net stream allowing it to be used with MPXJ
      /// where a Java InputStream is expected.
      /// This code is based on DotNetInputStream.java from the Saxon project http://www.sf.net/projects/saxon
      /// Note that I've provided this class as a convenience so there are a matching pair of
      /// input/output stream wrapper shopped with MPXJ. IKVM also ships with an input stream wrapper:
      /// ikvm.io.InputStreamWrapper, which you could use instead of this one.
      /// </summary>
      

      您应该能够使用此类来实现您在问题中描述的内容。

      【讨论】:

        最近更新 更多