OpenXml Unable to open package because FileMode or FileAccess is not valid for stream

The stream comes from the html form via ajax var jqXHR = data.submit();

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

I get this error:

The package cannot be opened because the FileMode or FileAccess value is not valid for the stream.

I google Cannot open the package because the FileMode or FileAccess value is not valid for the stream, but cannot find anything useful. Any ideas?

PS: Initially, I simplified the code to be posted here a lot. An if statement has been added to erase Sten Petrov's anxiety. I hope Request.File.count> 0 draws attention to its problem ... I still have the same problem ...

UPDATE

As a job, I followed the tip below and saved the file in a directory, then I use openxml to read it from the directory

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

then WordprocessingDocument.Open has an implementation for the file path, so WordprocessingDocument.Open(path); I hope you understand what I have done for future people who have problems.

+7
c # openxml openxml-sdk
source share
3 answers

What you do requires problems, because the request stream may not be fully loaded.

I suggest you first upload the file to MemoryStream or as a file, see here for the last option, then do whatever you want the downloaded file.

+3
source share

I believe that the stream was not opened properly with read or read access.

From MSDN about the WordprocessingDocument.Open method (thread, boolean)

IOException: Thrown when the "stream" does not open with Read (ReadWrite) access.

+3
source share

The WordprocessingDocument.Open method WordprocessingDocument.Open defined as:

 public static WordprocessingDocument Open(Stream stream, bool isEditable) 

You pass the value readStream.CanRead as the second parameter. This does not seem right to me. If CanRead is true , indicating that the stream can be read, you are trying to open WordprocessingDocument as editable, which apparently does not support the stream. I would just pass false for the second parameter. Otherwise, pass readStream.CanWrite , but do not be surprised if this property always returns false (as I would expect when working with streams from downloaded files).

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

+2
source share

All Articles