Share via


Disk error during write operaation

Question

Thursday, June 30, 2016 8:46 AM | 1 vote

Hello,

Im using EPPlus and I need to parse an excel file.

This is the code:

var postedFile = httpRequest.Files[file];Stream stream=postedfile.InputStreamif (!filename.Contains("xlsx") && !filename.Contains("xls")) return false;
                package = new ExcelPackage(stream);
                if (package.Workbook == null || package.Workbook.Worksheets.Count == 0)
                    return false;

In the bold line I get the exception:

"A disk error occurred during a write operation. (Exception from HRESULT: 0x8003001D (STG_E_WRITEFAULT))"

stacktrace:

"   at OfficeOpenXml.Utils.CompoundDocument.ILockBytes.WriteAt(Int64 ulOffset, IntPtr pv, Int32 cb, UIntPtr& pcbWritten)\r\n   at OfficeOpenXml.Utils.CompoundDocument.GetLockbyte(MemoryStream stream)\r\n   at OfficeOpenXml.ExcelPackage.Load(Stream input, Stream output, String Password)\r\n   at OfficeOpenXml.ExcelPackage..ctor(Stream newStream)\r\n   .....

Any help?

Thank You.

All replies (17)

Thursday, June 30, 2016 9:15 AM

Does this happen even in case of simplest new Excel files?

According to some articles, the problem can be caused by workbook protection. Try removing any protection and passwords, or specify the password after the stream parameter.


Thursday, June 30, 2016 9:27 AM

I created a new Excel file, and copied the data into the new file. But it still failed.

Thank you very much,

 


Thursday, June 30, 2016 12:50 PM

I can't tell whether you are working with an .xlsx or .xls file, but I doubt that .xls would be supported since it is not Office XML.

Paul ~~~~ Microsoft MVP (Visual Basic)


Thursday, June 30, 2016 1:04 PM

Im working with xlsx


Thursday, June 30, 2016 1:21 PM

Im working with xlsx

I'm not familiar with this library but in the only example I see the ExcelPackage constructor expects an existing file path and not a Stream. Is there an overloaded constructor that also accepts a Stream?

Paul ~~~~ Microsoft MVP (Visual Basic)


Thursday, June 30, 2016 1:26 PM

ExcelPackage(Stream)

Create a new instance of the ExcelPackage class based on a stream

Parameters

newStream

    **Type: **System.IO.Stream

    The stream object can be empty or contain a package. The stream must be Read/Write


Thursday, June 30, 2016 2:55 PM

I believe that you may need to read from the InputStream. See if the below link helps:

http://stackoverflow.com/questions/17955325/opening-a-client-side-excel-file-using-epplus

Paul ~~~~ Microsoft MVP (Visual Basic)


Thursday, June 30, 2016 4:52 PM

If you have time, and since the third-party sources are available, maybe you can investigate the error details. Seems that the Load function of ExcelPackage (http://epplus.codeplex.com/SourceControl/latest#EPPlus/ExcelPackage.cs) intercepts an exception, which is then lost because of other errors that happen inside the catch block.

Another option is reporting the issue to authors (see the ISSUES and DISCUSSIONS tabs), describing a reproducible scenario.

If possible, try files made in different versions of Excel, and also tray loading a file from local disk, not from uploading stream.

Or save the postedfile.InputStream to some file, then check it using Excel.


Sunday, July 3, 2016 6:11 AM

Hi,

Thank you very much, the problem is that there are files that works in that way, without Read function from Input stream.


Sunday, July 3, 2016 6:13 AM

Thank you for your answer,

I read and I saw that the exception occurred in Write operation:

internal static void CopyStream(Stream inputStream, ref Stream outputStream)
        {
            if (!inputStream.CanRead)
            {
                throw (new Exception("Can not read from inputstream"));
            }
            if (!outputStream.CanWrite)
            {
                throw (new Exception("Can not write to outputstream"));
            }
            if (inputStream.CanSeek)
            {
                inputStream.Seek(0, SeekOrigin.Begin);
            }

                const int bufferLength = 8096;
                var buffer = new Byte[bufferLength];
                lock (_lock)
                {
                    int bytesRead = inputStream.Read(buffer, 0, bufferLength);
                    // write the required bytes
                    while (bytesRead > 0)
                    {
                        outputStream.Write(buffer, 0, bytesRead);
                        bytesRead = inputStream.Read(buffer, 0, bufferLength);
                    }
                    outputStream.Flush();
            }
        }

Sunday, July 3, 2016 10:58 AM

I checked the exception that thrown by the internal functions, and its an exception of type : "Out of memory exception".

I think that its a problem with the MemoryStream.

Any suggestions?


Tuesday, July 5, 2016 4:15 PM

Hi,

Thank you very much, the problem is that there are files that works in that way, without Read function from Input stream.

So if I understand what you are saying, using the Read method does not work either?

If *your* code works for some files but not others then perhaps it's an issue with the uploaded file size.

Paul ~~~~ Microsoft MVP (Visual Basic)


Wednesday, July 6, 2016 9:17 AM

Hi,

Yes its an issue with the uploaded file size, when file size is bigger than something like 40 MB it doesnt work.


Friday, July 8, 2016 6:41 AM

Hi Coral Elimelech,

>>when file size is bigger than something like 40 MB it doesnt work.

I am not sure that this is a bug,  could you please provide a reproducing demo for test. If yes we could post the issue on Microsoft connect web site.

Best Regards,

Hart

We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.


Tuesday, November 1, 2016 2:19 PM

Hi Coral,

If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help members if they faces similar issue.

Thanks,
Sabah Shariq

[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


Tuesday, June 13, 2017 2:09 PM

I've encountered this same error occurring when trying to read a 4900-byte CSV file. It must be a bug in the EPP library.


Friday, January 26, 2018 10:57 AM

Hi,

I faced with the same issue and spent couple days with it.

In my case I received mentioned exception due to encryption.

I was able to read .xlsx file by passing password. In my case empty string "" was enough.

in your case please try to initialize package using constructor with password:

public ExcelPackage(Stream newStream, string Password)
package = new ExcelPackage(stream, "");

Have a look into ExcelPackage source code http://epplus.codeplex.com/SourceControl/latest#EPPlus/ExcelPackage.cs

There is a method

private void Load(Stream input, Stream output, string Password) which is used to load excel file.private void Load(Stream input, Stream output, string Password) ...

if (Password != null)

{
  Stream encrStream = new MemoryStream();
  CopyStream(input, ref encrStream);
  EncryptedPackageHandler eph = new EncryptedPackageHandler();
  Encryption.Password = Password;
  ms = eph.DecryptPackage((MemoryStream)encrStream, Encryption);
}
else
{
  ms = new MemoryStream();
  CopyStream(input, ref ms);
 }...Code will try to decrypt excel stream even if password is empty, BUT NOT NULL.

However if you try to initialize package for file that is not encrypted you will have exception:

'The stream is not an valid/supported encrypted document.'