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

  • Question

  • 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

    Saturday, October 19, 2019 11:57 AM

All replies

  • Best bet is to ask in the ASP.NET Forums and the Drive API Client library docs. Questions specific to Google Drive should be asked here.

    Why redirect you to the above? Because this forum is for addressing C# questions, not third party libraries.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Saturday, October 19, 2019 2:57 PM