分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

上传图片用图片文件的对象hash哈希值判断图片是否一样,避免重复提交相同的图片到服务器中

发布时间:2023-09-06 01:07责任编辑:白小东关键词:上传图片

上传图片用图片文件的对象hash哈希值判断图片是否一样,避免重复提交相同的图片到服务器中

 ???????/// <summary> ???????/// 上传企业logo ???????/// </summary> ???????/// <returns></returns> ???????public ActionResult UploadLogo(string comid) ???????{ ???????????HttpFileCollection files = System.Web.HttpContext.Current.Request.Files; ???????????if (files.Count == 0) return Json("没有没文件", JsonRequestBehavior.AllowGet); ???????????MD5 hash = new MD5CryptoServiceProvider(); ???????????/**计算指定stream对象的哈希值**/ ???????????byte[] bytehashValue = hash.ComputeHash(files[0].InputStream); ???????????var HashData = BitConverter.ToString(bytehashValue).Replace("-", ""); ???????????var FileExtension = Path.GetExtension(files[0].FileName); ???????????var filename = comid + "_" + HashData + FileExtension; ???????????var virtualpath = string.Format("/Upload/Logo/{0}/{1}", DateTime.Now.ToString("yyyyMMdd"), filename); ???????????//将虚拟路劲转换成物理路劲 ???????????var fullpath = Server.MapPath(virtualpath); ???????????var path = Path.GetDirectoryName(fullpath); ???????????if (!Directory.Exists(path)) ???????????????Directory.CreateDirectory(path); ???????????if (!System.IO.File.Exists(fullpath)) ???????????????files[0].SaveAs(fullpath); ???????????var FileSize = this.FileSize(files[0].ContentLength); ???????????return Json(new { FileName = filename, FilePath = virtualpath, FileSize = FileSize }, "text/html", JsonRequestBehavior.AllowGet); ???????} ???????/// <summary> ???????/// 计算文件大小 ???????/// </summary> ???????/// <param name="bytes"></param> ???????/// <returns></returns> ???????public string FileSize(long bytes) ???????{ ???????????long kblong = 1024; ???????????long mblong = 1024 * 1024; ???????????if (bytes < kblong) ???????????????return decimal.Round(decimal.Divide(bytes, kblong), 2).ToString() + "KB"; ???????????else ???????????????return decimal.Round(decimal.Divide(bytes, mblong), 2).ToString() + "MB"; ???????}

获取文件的hash哈希值方法:

 ???????/// <summary> ???????/// 计算文件的hash值 用于比较两个文件是否相同 ???????/// </summary> ???????/// <param name="filePath">文件路径</param> ???????/// <returns>文件hash值</returns> ???????public static string GetFileHash(string filePath) ???????{ ???????????//创建一个哈希算法对象 ????????????using (HashAlgorithm hash = HashAlgorithm.Create()) ???????????{ ???????????????using (FileStream file = new FileStream(filePath, FileMode.Open)) ???????????????{ ???????????????????//哈希算法根据文本得到哈希码的字节数组 ????????????????????byte[] hashByte= hash.ComputeHash(file); ???????????????????//将字节数组装换为字符串 ?????????????????????return BitConverter.ToString(hashByte); ???????????????} ???????????} ???????}

做一个记录,没有高水平技术,简简单单写个博客!

上传图片用图片文件的对象hash哈希值判断图片是否一样,避免重复提交相同的图片到服务器中

原文地址:http://www.cnblogs.com/axinno1/p/7460940.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved