简体   繁体   中英

Is the C# library MPXJ able to read in a file from a MemoryStream?

I am using the library MPXJ which works great but I now want the ability for users to upload their own file (asp.net-mvc site) and it comes on the server side form post as a HttpPostedFileBase and I then I convert to a memory stream using:

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

Given that, I am trying to figure out how i can read it in as a MemoryStream (versus a file location on disk)

Right now I have something like this:

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

and i wanted to have something like this:

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

Is this possible "natively" or do i need to save the file on my server and then read in from there (trying to avoid that option)?

The MPPReader.Read() method only accepts 4 types of parameters, none of which are a MemoryStream and all but one seem to be types that are defined within the library itself:

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

You are currently using the string parameter as it expects a path, however it seems the closest that you might get would be to try to copy your existing MemoryStream object to the InputStream type found within the library and using that (if that type of support exists).

MPXJ ships with a pair of classes called DotNetInputStream and DotNetOutputStream which act as wrappers around .Net streams so that they can be used where MPXJ is expecting Java InputStream or OutputStream .

Here is the relevant comment from 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>

You should be able to use this class to achieve what you describe in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM