none
HttpPostedFile and streams and MD5 RRS feed

  • Question

  • In the past, I used the following code to calculate an MD5 signature on a file:

            Dim stream As FileStream = Nothing
            stream = New FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read) 
            Dim C() As Byte = md5.ComputeHash(stream)
            ComputeMD5Hash = BitConverter.ToString(C)

    This required a physical file to exist.

    I want to perform the same operation on an HttpPostedFile posted from ajax to a code behind module.

    In particular, I do not want to save the HttpPostedFile to file as we have issues with av scans holding file handles, etc...
    When I save 
    HttpPostedFile to file, it works as it is supposed, however, I would like to avoid that step.
    Since md5.ComputeHash takes a byte array, I am wondering how I can convert HttpPostedFile  to a byte array

    Thank you


    GADOI


    I can convert HttpPostedFile.InputStream to a system.io.stream but then how do I convert that to a bye array...
    • Edited by GAINSCOM Wednesday, October 3, 2018 1:31 PM
    Wednesday, October 3, 2018 1:17 PM

Answers

  • You can use MemoryStream to get bytes in stream. First use Stream.CopyTo method to copy content of source stream to target stream that is MemoryStream, then call MemoryStream.ToArray to get content of stream as byte array.

    • Marked as answer by GAINSCOM Wednesday, October 3, 2018 1:58 PM
    Wednesday, October 3, 2018 1:52 PM