2013-07-26 17 views
7

流通過AJAX來自HTML表單var jqXHR = data.submit();的OpenXML無法打開包,因爲的FileMode或FileAccess的值是無效的流

public static GetWordPlainText(Stream readStream,string filePath) 
{ 
    WordprocessingDocument.Open(readStream, readStream.CanRead); 
} 
[HttpPost] 
public ActionResult FileUpload() 
{ 
var MyFile = Request.Files[0]; 
if (Request.Files.Count > 0 && MyFile != null) 
{ 
    GetWordPlainText(Request.InputStream); 
} 
} 

我得到這個錯誤:

Cannot open package because FileMode or FileAccess value is not valid for the stream.

我谷歌無法打開包,因爲FileMode或FileAccess值對於流無效,但找不到任何有用的內容。有任何想法嗎? PS:最初我簡化了要發佈到這裏的代碼。增加了if語句,以便消除Sten Petrov的擔憂。我希望Request.File.count> 0確實解決他的關心......我仍然有同樣的問題...

UPDATE

作爲一個工作,我身邊跟着下面的意見和文件保存到一個目錄然後我用OpenXML可以從目錄

var MyFile = Request.Files[0]; 
    var path = Path.Combine(Server.MapPath("~/App_Data/temp"), MyFile.FileName); 
       using (MemoryStream ms = new MemoryStream()) 
       { 
        //if file exist plz!!!! TODO 

        Request.Files[0].InputStream.CopyTo(ms); 
        System.IO.File.WriteAllBytes(path, ms.ToArray()); 
       } 

然後WordprocessingDocument.Open讀它有一個文件路徑,從而實現 WordprocessingDocument.Open(path);希望你得到了什麼我做了有問題的未來人們的觀念。

回答

3

你在做什麼是要求麻煩,因爲請求流可能沒有完全下載。

我建議你先將文件下載到MemoryStream中或作爲文件,see here用於後者的選項,然後做任何你想要的上傳文件。

3

我猜這個流沒有正確打開讀或寫訪問。

public static WordprocessingDocument Open(Stream stream, bool isEditable) 

要傳遞的readStream.CanRead值作爲第二個參數:

MSDN約WordprocessingDocument.Open方法(流,布爾值)

IOException: Thrown when "stream" is not opened with Read (ReadWrite) access.

+0

我會研究這個可能的解決方案。因爲知道我做了彼得羅夫推薦的東西。 – hidden

2

的方法WordprocessingDocument.Open如被定義。這對我來說似乎不正確。當CanReadtrue,表明可以讀取流,您試圖打開WordprocessingDocument作爲可編輯的流,該流可能不支持。我只想通過false獲取第二個參數。否則,通過readStream.CanWrite,但不要感到驚訝,如果此屬性總是返回false(正如我在處理來自上傳文件的流時所預期的那樣)。

http://msdn.microsoft.com/en-us/library/office/cc536138.aspx

+0

WordprocessingDocument.open有幾個重載,一個是流,布爾。但是,我會嘗試將其設置爲false,並讓您知道我是否能夠使用流而不是跛腳寫入文件,然後使用WordprocessingDocument.open的字符串重載 – hidden

+0

對不起,是的。我打算髮布'(Stream,bool)'方法簽名並得到了錯誤的簽名。剛解決這個問題。我試圖做的一點是,當這個方法的第二個參數是'true'時,它需要基礎流是可編輯的,而你上傳的流不是。 –

相關問題