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>") 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>") Response.Write("File integrity cannot be verified " & _ "and will not be saved.") End If ... %>
|
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
|
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; } } }
|