I am trying to create MD5 of the file when user uploads it to my server and forward that request along with posted file to my service which rechecks the MD5 of the posted file using the following method.
This always shows different length for Request.Files[0].InputStream on the service end. Is there something i am missing as to why this would show incorrect length for posted file?
if (context.Request.Files.Coun开发者_运维技巧t > 0)
{
byte[] fileData = null;
using (var binaryReader = new BinaryReader(context.Request.Files[0].InputStream))
{
fileData = binaryReader.ReadBytes((int)context.Request.Files[0].InputStream.Length);
binaryReader.Close();
}
using (MD5 md5 = MD5.Create())
{
byte[] hashData = md5.ComputeHash(fileData);
//loop for each byte and add it to StringBuilder
for (int i = 0; i < hashData.Length; i++)
{
FileMD5Hash.Append(hashData[i].ToString());
}
}
}
This was not the right way to create byte[]. I used memorystream's toArray(), and it's working beautifully for me.
精彩评论