0

My primary requirement is to get length (File Size) of a task stream System.Threading.Tasks.Task<System.IO.Stream>. In a memorystream i used to get the file size using "stream.Result.Length" but when i tried to use the same in a taskstream it throws an exception saying System.NotSupportedException, Seems like the stream doesn't support that property. I think there is a difference between memory streams and other streams.

Exception occurred handling notification:System.NotSupportedException: This stream does not support seek operations.

Could you please give me any instructions how can i achieve this i found this link which gives me the instructions. I am using .Net 3.5 therefore i cant use ConvertTo() functions that is there in .Net 4

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Dark99
  • 81
  • 3
  • 11
  • show the code you tried – user1666620 Oct 21 '15 at 15:48
  • 1
    Not all streams support `Length`. See [Stream.Length throws NotSupportedException](http://stackoverflow.com/questions/3373579/stream-length-throws-notsupportedexception). Maybe yours doesn't. – dbc Oct 21 '15 at 15:54

1 Answers1

2

The point of a stream is that you don't need to have all the data available before you can start processing the first part of it. MemoryStream is an exception, in that it does have the entire contents of the stream in memory at the same time, so it can "support seek operations" to tell you things like how big the stream is.

If you need the full size of a stream that can't seek, you're going to have to consume the entire stream to find that out. If the stream is always going to be relatively small, you could copy its contents into a MemoryStream. If it's going to be larger, you could copy its contents into a file on disk.

You may want to examine why you need to know the length. If it's so that you can cancel uploads that are too large, for example, then perhaps you should just start processing the upload in chunks, but after each piece of data comes in check how much data you've received so far, and cancel the process if it gets too big.

Community
  • 1
  • 1
StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • Thankyou verymuch for the explanation. I am creating this stream using a pdf file. so as you have said i need to convert or copy the task-stream into a another stream like memory stream right, There was these inbuilt methods 'CopyStreamToStreamAsync()' or 'file.Stream.CopyTo()' methods which can be used in .net4.0+ , but i am using .net 3.5. Anyway there was few suggestions given for .net 3.5 as well but still dodn't workout for me. any way what you say is to read the whole stream to and to get the length of it ?? – Dark99 Oct 21 '15 at 16:20
  • @Dark99: Why do you need the stream's length? Where is the PDF file coming from? – StriplingWarrior Oct 21 '15 at 16:30
  • i am keeping a variable "attachmentMetadata" to store the details of the pdf , such as name of the file.. etc with that i also need to store the size of the file. as i have said in the question , for such instance i have use this to get the size of the stream "stream.Result.Length" before but now i am encountering a problem as get a task stream. – Dark99 Oct 21 '15 at 16:46
  • @Dark99: Is the PDF file on disk? If so, you could just check the file size. Does the attachmentMetadata need to have the length prior to your processing the rest of the stream? If not, you could process the PDF file and keep track of how many bytes are passing through as you go, and then set the metadata length at the end. – StriplingWarrior Oct 21 '15 at 17:09