11

The stream comes from an 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:

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

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

PS: Initially I simplified the code to be posted here to much. Added the if statement so that it would erase the concern by Sten Petrov. I hope Request.File.count>0 does address his concern... I still have the same problem...

UPDATE

As a work around I followed the advise below and save the file to 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 a implementation for filepath so WordprocessingDocument.Open(path); hope you get the idea of what I did for future people that have problems.

Chris
  • 8,527
  • 10
  • 34
  • 51
hidden
  • 3,216
  • 8
  • 47
  • 69

4 Answers4

6

What you're doing is asking for trouble, because the Request stream may not have fully been downloaded.

I suggest you download the file first into a MemoryStream or as a file, see here for the latter option, then do whatever you want to the uploaded file.

Sten Petrov
  • 10,943
  • 1
  • 41
  • 61
3

I guess the stream is not correctly opened with read or readwrite access.

From MSDN about WordprocessingDocument.Open method (Stream, Boolean)

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

Chris
  • 8,527
  • 10
  • 34
  • 51
2

The method WordprocessingDocument.Open is defined as:

public static WordprocessingDocument Open(Stream stream, bool isEditable)

You're passing the value of readStream.CanRead as the second parameter. This doesn't seem correct to me. When CanRead is true, indicating that the stream can be read, you're trying to open the WordprocessingDocument as editable, which the stream probably doesn't support. I would just pass false for the second parameter. Otherwise, pass readStream.CanWrite but don't be surprised if this property always returns false (as I would expect when dealing with streams from uploaded files).

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

Michael Gunter
  • 12,528
  • 1
  • 24
  • 58
  • WordprocessingDocument.open has several overloads one namely being stream,bool. However, i will try to set it to false and let you know if I was able to use the stream instead of the lame write to file then use the string overload of WordprocessingDocument.open – hidden Jul 26 '13 at 20:45
  • Sorry, yes. I meant to post the `(Stream, bool)` method signature and got the wrong one. Just fixed that. The point I was trying to make is that when the second parameter to this method is `true`, it requires the underlying stream to be editable, and your uploaded stream is not. – Michael Gunter Jul 26 '13 at 21:03
1

I had same issue but with ClosedXML.Excel library. I was downloading file using simple WebRequest instance and I my problem was that Stream got closed with WebRequest disposal so I had to copy it. For the "safety" I used MemoryStream.

MemoryStream memStream = new MemoryStream();

using (WebResponse response = request.GetResponse())
{
     response.GetResponseStream()?.CopyTo(memStream);
}

XLWorkbook workbook = new XLWorkbook(memSream);

CopyTo is to ensure stream is available after WebRequest is disposed.

Diomos
  • 420
  • 5
  • 15