string ImageFileName = null;
string StrImageName = null;
string UploadImageFolderPath = Server.MapPath("../Upload/Images/");
string ImageFileExtension;
if (ImageUpload.HasFile)
{
StrImageName = ImageUpload.FileName.ToString();
ImageFileExtension = System.IO.Path.GetExtension(StrImageName);
// GIVE A UNIQUE FILENAME(MD5) TO THE UPLOADED FILE:
MD5CryptoServiceProvider objMD5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(StrImageName);
bs = objMD5.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString("x2").ToLower());
}
StrImageName = s.ToString();
ImageFileName = StrImageName + ImageFileExtension;
// As long as the Image-File-Name is exists in the folder then we are MD5ing again the imgage-file-name:
while (System.IO.File.Exists(UploadImageFolderPath + ImageFileName))
{
MD5CryptoServiceProvider objMD5While = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bsWhile = System.Text.Encoding.UTF8.GetBytes(StrImageName);
bsWhile = objMD5.ComputeHash(bsWhile);
System.Text.StringBuilder sWhile = new System.Text.StringBuilder();
foreach (byte bWhile in bsWhile)
{
sWhile.Append(bWhile.ToString("x2").ToLower());
}
StrImageName = sWhile.ToString();
ImageFileName = StrImageName + ImageFileExtension;
}
ImageUpload.PostedFile.SaveAs(UploadImageFolderPath + ImageFileName);
}
沒有留言:
張貼留言
留個話吧:)