I want to check MD5 hash without saving it,
to check if it exists already i have tried this code below but it just wont compute the MD5 of the file if its not saved already.
so i was thinking about filling the file in a byte array then get that byte array MD5 but that will leave me with no bytes in the file.
Any suggestions ? this is my code
public void ProcessRequest(HttpContext context)
{
if (context.Request.Files.Count > 0)
{
HttpFileCollection files = context.Request.Files;
for (int i = 0; i < files.Count; i++)
{
HttpPostedFile file = files[i];
string ServerPath = context.Server.MapPath("~/uploads/" + file.FileName);
string Name = file.FileName;
string MD5 = GetMD5(ServerPath);
int Size = file.ContentLength;
string type = file.ContentType;
file.SaveAs(ServerPath);
}
}
}
This is getMD5
Method
public string GetMD5(string fileName)
{
try
{
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(fileName))
{
string Hash = BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "");
return Hash;
}
}
}
catch (Exception ex)
{
throw ex;
}
}