Reading stream twice?

后端 未结 2 988
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-31 16:26

When I have uploaded an image from my website I need to do 2 things:

  1. read the image dimensions
  2. save the image to the database

the first th

2条回答
  •  离开以前
    2021-01-31 17:18

    In addition to Jon's answer: If you have a StreamReader, there is no Position parameter - instead you need to access its BaseStream.

    For this purpose you can use a little extension method:

    public static void ResetStreamReader(this StreamReader sr, long position = 0)
    {
        if (sr == null) return;
        sr.BaseStream.Position = position;
    }
    

    Put it inside your public static Extension class, then you can use it like so:

    sr.ResetStreamReader();
    

    Or you can pass the desired start position, if you want to start with a position > 0, for example if you want to skip headers when you read the stream again.

提交回复
热议问题