Reading stream twice?

后端 未结 2 993
佛祖请我去吃肉
佛祖请我去吃肉 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:03

    Well, the simplest way is:

    file.InputStream.Position = 0;
    

    ... assuming the stream supports seeking. However, That may do interesting things to the Image if you're not careful - because it will have retained a reference to the stream.

    You may be best off loading the data into a byte array, and then creating two separate MemoryStream objects from it if you still need to. If you're using .NET 4, it's easy to copy one stream to another:

    MemoryStream ms = new MemoryStream();
    Request.Files["logo"].InputStream.CopyTo(ms);
    byte[] data = ms.ToArray();
    

提交回复
热议问题