2011-10-06 71 views
0

我试图在用户将文件上传到我的服务器时创建文件的MD5,并将该请求连同发布的文件一起转发给我的服务,该服务重新检查发布的MD5文件使用以下方法。Request.Files [0] .InputStream在上传后显示不同的长度

这总是显示服务端的Request.Files [0] .InputStream的长度不同。有什么我失踪,为什么这会显示不正确的张贴文件的长度?

if (context.Request.Files.Count > 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()); 
       } 
      } 
     } 

回答

0

这不是创建byte []的正确方法。我使用了memorystream的toArray(),它对我来说工作得很好。