This is a migrated thread and some comments may be shown as answers.

Validate File after Upload completed

2 Answers 54 Views
Upload
This is a migrated thread and some comments may be shown as answers.
Kristian Kretschmer
Top achievements
Rank 1
Kristian Kretschmer asked on 08 Oct 2010, 06:29 PM
Hello,

i search for a method to validate the file, aber upload ist completed. Is a solution available to create something like a hashkey, before upload started and compare these hashkey with the uploaded file?

Thank you.

2 Answers, 1 is accepted

Sort by
0
Kristian Kretschmer
Top achievements
Rank 1
answered on 08 Oct 2010, 07:30 PM
Okay, here is my solution:

i added a FileHashKey Parameter on the FileUploadStarting Event

 

 

 

void RadUpload1_FileUploadStarting(object sender, Telerik.Windows.Controls.FileUploadStartingEventArgs e)
{
  
if (e.FileParameters.ContainsKey("FileHashKey"))
{
e.FileParameters["FileHashKey"] = MD5FileHash(e.SelectedFile.File);
}
else
{
e.FileParameters.Add("FileHashKey", MD5FileHash(e.SelectedFile.File));
}
}

a function to create the hashkey
i found a MD5CryptoServiceProvider implementation for silverlight here:
http://www.markharris.net.au/blog/2008/10/23/md5cryptoserviceprovider-for-silverlight/

 

private string MD5FileHash(FileInfo fiFile)
{
        try
        {
            FileStream FileCheck = fiFile.OpenRead();
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] md5Hash = md5.ComputeHash(FileCheck);
            FileCheck.Close();
            FileCheck = null;
            string Berechnet = BitConverter.ToString(md5Hash).Replace("-", "").ToLower();
            md5Hash = null;
            return Berechnet;
        }
        catch (Exception)
        {
            return String.Empty;
        }
}


on serverside i compare the hashkeys in the 

GetAssociatedData() method

 

 

 

 

 

   

string HashKey = this.Request.Form["0_FileHashKey"];
string localHashKey = String.Empty;
if (HashKey != String.Empty)
{
    localHashKey = MD5FileHash(MYSERVERFILEPATHHERE);
}

 

 

if (HashKey != localHashKey)
{
//FILE IS CORRUPT
}

i think, that's a good way to validate the file is complete.

 

0
Alex Fidanov
Telerik team
answered on 11 Oct 2010, 10:22 AM
Hi Kristian Kretschmer,

Thank you for sharing your solution with us. Please let us know if we can assist you with anything else on the matter.

Best wishes,
Alex Fidanov
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Upload
Asked by
Kristian Kretschmer
Top achievements
Rank 1
Answers by
Kristian Kretschmer
Top achievements
Rank 1
Alex Fidanov
Telerik team
Share this question
or