简体   繁体   English

将文件保存到计算机之前检查MD5

[英]Check MD5 Before Saving file to machine

I want to check MD5 hash without saving it, 我想检查MD5哈希而不保存它,

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. 要检查它是否已经存在,我已经在下面尝试过此代码,但是如果尚未保存该文件,它将不会计算该文件的MD5。

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. 所以我在考虑将文件填充到字节数组中,然后获取该字节数组MD5,但这将使我在文件中没有字节。

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 这是getMD5方法

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;
    }
}

Since the bottleneck here is the download, you don't have any real advantage in not saving the file before checking the hash. 由于这里是下载的瓶颈,因此在检查哈希之前不保存文件并没有任何真正的优势。 What you may want to do is to download into your temp folder and then eventually delete it yourself or let the OS clean it if not needed. 您可能想要做的是下载到您的temp文件夹中,然后最终自己删除它,或者在不需要时让操作系统清除它。

Check this answer for information about how to use the temp folder and temporary files: Writing File to Temp Folder 检查此答案以获取有关如何使用临时文件夹和临时文件的信息: 将文件写入临时文件夹

One more complex option is to host a page on the same server where files are stored and let it calculate the MD5: this way you can check the MD5 before downloading. 一个更复杂的选项是在存储文件的同一服务器上托管一个页面,并让它计算MD5:通过这种方式,您可以在下载前检查MD5。 Otherwise, since the hash is a function of the physical bytes composing your file you need the whole file (downloaded or memory stored) to compute it. 否则,由于哈希是构成文件的物理字节的函数,因此需要整个文件(下载或存储的内存)来进行计算。

You can use the InputStream property in the HttpPostedFile class. 您可以在HttpPostedFile类中使用InputStream属性。 And to compute a hash, use the ComputeHash method of the HashAlgorithm class. 要计算哈希,请使用HashAlgorithm类的ComputeHash方法。

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];
            var hasAlg=HashAlgorithm.Create("MD5");
            var MD5= hasAlg.ComputeHash(file.InputStream);

        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM