C#库MPXJ是否能够从MemoryStream读取文件



我使用的是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);

这可能是"本机"的吗?还是我需要将文件保存在服务器上,然后从那里读取(尽量避免这种选择)?

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

  • java.io.File
  • java.io.InputStream
  • org.apache.poi.pofs.filesystem.POIFSFileSystem
  • string

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

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>

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

最新更新