ReadTimeout threw an exception when converting byte array to Stream

前端 未结 4 664
独厮守ぢ
独厮守ぢ 2021-01-20 06:33

I got this error

ReadTimeout = \'((System.IO.Stream)(ms)).ReadTimeout\' threw an exception of type \'System.InvalidOperationException\'.

M

相关标签:
4条回答
  • 2021-01-20 06:55

    You should be careful about the type of image in database, if your image is too big then you must use LONGBLOB and not BLOB.

    0 讨论(0)
  • 2021-01-20 07:12

    I used the following code and I needed the MemoryStream to be used as email attachment:

    string filename=@"C:\images\myimage.img"
    MemoryStream result = new MemoryStream();
    MemoryStream source = new MemoryStream(File.ReadAllBytes(filename));
    source.WriteTo(result);
    
    0 讨论(0)
  • 2021-01-20 07:13

    The ReadTimeout property must be overridden, in the base System.IO.Stream class it always throw System.InvalidOperationException error by design.

    The solution is not to cast ms to the base type when reading the timeout:

    int readTimeout = ms.ReadTimeout;
    

    Edit: didn't check before posting.. MemoryStream also does not override that property - meaning timeout for such stream is not implemented.

    You have to either use other implementation of Stream class that does override the ReadTimeout property, or write your own implementation.

    0 讨论(0)
  • 2021-01-20 07:17

    You can use the ReadTimeout/WriteTimeout properties only if the stream support timing out. You can check this via the CanTimeout property. If the CanTimeout property returns false all access to these properties should raise an InvalidOperationException.

    0 讨论(0)
提交回复
热议问题