How to generate hash value of file in asp.net to determine if file exists in Google Drive

  • Thread starter Thread starter Jason Phang
  • Start date Start date
J

Jason Phang

Guest
I have a program whereby I will be able to upload, download, delete files to my Google Drive. Now, the issue that I am encountering is that I am able to generate the hash value of each files but I do not know how to check the hash value of the file that i want to upload with the hash value of the existing files in the Google Drive. If there is an existing hash already, then a message pop-up should appear and inform the user that the file is a duplicate and the user would have to reupload another file. I am unsure on where or how to insert the code for generating the respective hash alongside the message pop-up.

This is the code for my GetGoogleDrive.cshtml file.

@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<p>
<label for="file">Upload file:</label>
<input type="file" name="file" id="file" />
<input type="submit" value="Upload" id="fileUpload" />
<script>
$('#fileUpload').click(function (e) {
if ($('#file').val() === "") {
Swal.fire({
title: 'Nothing',
text: 'No file selected',
type: 'error',
confirmButtonText: 'Again!!',
timer: 4000

})
}
else {
Swal.fire({
title: 'Wait awhile...',
text: 'File will be uploaded shortly',
type: 'success',
confirmButtonText: 'Okay, cool',
timer: 4000
})
}

});
</script>


This is the code for my upload controller class.

[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file)
{
GoogleDriveFilesRepository.FileUpload(file);
return RedirectToAction("GetGoogleDriveFiles");
}


And this is the code for my Google Drives modal class.

public static void FileUpload(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
DriveService service = GetService();

string path = Path.Combine(HttpContext.Current.Server.MapPath("~/GoogleDriveFiles"),
Path.GetFileName(file.FileName));
file.SaveAs(path);

HashGenerator(path);
compareHash(HashGenerator(path));

var FileMetaData = new Google.Apis.Drive.v3.Data.File();
FileMetaData.Name = Path.GetFileName(file.FileName);
FileMetaData.MimeType = MimeMapping.GetMimeMapping(path);

FilesResource.CreateMediaUpload request;

using (var stream = new System.IO.FileStream(path, System.IO.FileMode.Open))
{
request = service.Files.Create(FileMetaData, stream, FileMetaData.MimeType);
request.Fields = "id";
request.Upload();
}

}

}


Am I able to generate the hash from razor via JS or i have to do from c# side?




Jason

Continue reading...
 
Back
Top