2016-04-14 55 views
0

我使用the library MPXJ這很好,但我現在希望用戶能夠上傳自己的文件(asp.net-mvc網站),它作爲HttpPostedFileBase在服務器端表單發佈,然後我轉換到內存流使用:C#庫MPXJ能夠從MemoryStream中讀取文件嗎?

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); 

這是可能的「原生地」或者我需要將文件保存在我的服務器上,然後從那裏閱讀(試圖避免該選項)?

回答

1

MPPReader.Read()該方法只接受4種類型的參數,其中沒有一個是一個MemoryStream和所有,但一個似乎是被庫本身內定義的類型:

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

您目前正在使用的string參數,因爲它需要一個路徑,但它似乎是你可能會得到最接近將嘗試將現有MemoryStream對象複製到庫中找到的InputStream類型並使用它(如果存在這種類型的支持)。

1

MPXJ一般有一對稱爲DotNetInputStreamDotNetOutputStream類充當包裝器淨流,使得它們可以在那裏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> 

您應該能夠使用這個類來實現你在你的問題描述。