Description:
|
FileUp generates an MD5 hash of each uploaded file, after it is received on the
server. The Checksum property returns a string representing the specified file's
MD5 hash.
You can use this value to verify the integrity of the uploaded file, by comparing
an MD5 hash generated on the client, and the MD5 hash generated by FileUp on the
server.
Note: Note that FileUp generates an MD5 hash on the server,
not the client. To verify data integrity, you will need a tool that can generate
an MD5 hash on the client, such as SoftArtisans FileManager which is included free
with FileUp.
|
|
ASP |
<% '--- Instantiate the FileUp object Set fileUpload = Server.CreateObject("SoftArtisans.FileUp") '...
'--- Get the client-side hash submitted by the form clientHash = fileUpload.Form("FileHash")
'--- Now, make a server-side hash with FileUp serverHash = fileUpload.Form("MyFile").CheckSum
Response.Write("<b>Comparing hashes</b><br />") Response.Write("<b>Client hash:</b> " & clientHash & "<br
/>") Response.Write("<b>Server hash:</b> " & serverHash & "<br
/>")
'--- Compare them to verify file integrity If StrComp(clientHash, serverHash) = 0 Then '--- The hashes are the same! File integrity is verified fileUpload.Form("MyFile").Save Response.Write("... Hashes <b>OK!</b><hr noshade="noshade"
/>") Response.Write("File integrity has been verified, " & _
"and the file has been saved to: " &_
fileUpload.Form("MyFile").ServerName & "<br />") Else Response.Write("... Hashes <B>mismatch!</B>" &
_ "<hr noshade="noshade" />") Response.Write("File integrity cannot be verified " & _ "and will not be saved.") End If '... %>
|
C# |
//--- Process the upload private void ProcessUpload() { //--- Declare an object for FileUp FileUp fileUpload = null;
//--- This StringBuilder will be used to display results System.Text.StringBuilder res = new System.Text.StringBuilder();
try { //--- Instantiate the FileUp object fileUpload = new FileUp(Context);
//--- When the HttpModule is used, the path property does
not //--- affect caching location. Instead it is merely used
//--- as a placeholder to store the path to the directory //--- where the files will be saved. fileUpload.Path = "C:\\UploadDir";
//--- Get a reference to the uploaded file field SaFile file = (SaFile)fileUpload.Form["MyFile"];
//--- Check to be sure there was a file selected in the
form //--- If so, continue processing if (file != null) { if (!file.IsEmpty) { //--- Get the client-side hash submitted
by the form string clientHash = & fileUpload.Form["FileHash"].ToString();
//--- Now, make a server-side hash
with FileUp string serverHash = file.Checksum;
res.Append("Comparing hashes<br
/>"); res.Append("Client hash: " + clientHash
+ "<br />"); res.Append("Server hash: " + serverHash
+ "<br />");
//--- Compare them to verify file
integrity if (String.Compare(clientHash, serverHash)
== 0) { //--- File integrity
is verified file.Save(); res.Append ("... Hashes
OK!"); res.Append ("File integrity
has been verified, " + "and the
file has been saved to: " + file.ServerName
+ "<br />"); } else { //--- If the hashes
don't match, don't save the file //--- Explicitly delete
it file.Delete(); res.Append ("... Hashes
mismatch!"); res.Append ("File integrity
cannot be verified " + "and therefore
it will not be saved."); }
} else { //--- If IsEmpty is true, the file
field was left empty res.Append ("There was no file submitted
for upload."); } } else { res.Append ("The referenced field does not exist
" +
"or is not of type=\"file\""); }
//--- Display the results on the webform ltrUploadResults.Text = results.ToString();
} catch(Exception ex) { //--- If an exception is caught, display the details ltrUploadResults.Text = "An error has occurred:<br />"
+ ex.Message; } finally { //--- Always call FileUp.Dispose in the finally block if (fileUpload != null) { fileUpload.Dispose(); fileUpload = null; } } }
|
VB.NET |
'--- Process the upload Private Sub ProcessUpload()
'--- Declare an object for FileUp Dim fileUpload as FileUp
'--- This StringBuilder will be used to display results Dim res As New System.Text.StringBuilder()
Try
'--- Instantiate FileUp fileUpload = New FileUp(Context)
'--- When the HttpModule is used, the path property does not '--- affect caching location. Instead it is merely used
'--- as a placeholder to store the path to the directory '--- where the files will be saved. fileUpload.Path = "C:\UploadDir
'--- Get a reference to the uploaded file field Dim file As SaFile = CType (fileUpload.Form("MyFile"), SaFile)
'--- Check to be sure there was a file selected in the form '--- If so, continue processing If Not (file Is Nothing) Then If Not (file.IsEmpty) Then
'--- Get the client-side hash submitted by the
form Dim clientHash As String
clientHash = fileUpload.Form("FileHash").ToString()
'--- Now, make a server-side hash with FileUp Dim serverHash As String = file.Checksum
res.Append("Comparing hashes<br />") res.Append("Client hash: " + clientHash + "<br
/>") res.Append("Server hash: " + serverHash + "<br
/>")
'--- Compare them to verify file integrity If (String.Compare(clientHash, serverHash) =
0) Then
'--- File integrity is verified file.Save() res.Append ("... Hashes OK!") res.Append ("File integrity has
been verified, " + _ "and the file has been
saved to: " + _ "file.ServerName + "<br
/>") Else
'--- If the hashes don't match,
don't save the file '--- Explicitly delete it file.Delete() res.Append ("... Hashes mismatch!") res.Append ("File integrity cannot
be verified " + _
"and therefore it will
not be saved.") End If
Else '--- If IsEmpty is true, the file field was
left empty res.Append("There was no file submitted for
upload.") End If
Else results.Append ("The referenced field does not exist " +
_ "or is not of type=""file""") End If
'--- Display the results on the webform ltrUploadResults.Text = res.ToString()
Catch ex As System.Exception '--- If an exception is caught, display the details ltrUploadResults.Text = "An error has occurred:<br />" + _ ex.Message Finally '--- Always call FileUp.Dispose in the finally block If Not (fileUpload Is Nothing) Then fileUpload.Dispose() fileUpload = Nothing End If
End Try
End Sub
|
|