Jason Phang
Well-known member
- Joined
- Aug 13, 2019
- Messages
- 46
- Programming Experience
- Beginner
At the moment, my program is able to upload files and display alert messages to users when it is uploaded. The thing is now, after uploading, it is supposed to see if it is a duplicate file or not. If it is, then a message alert will inform the user that it is a duplicate while if it is new , then the user gets to upload the file and a message informing the user the file is uploaded is displayed. The thing is, I know there is something off with my logic but I am unable to find a way to wrap this up. As of now, when I upload a file, irregardless of new or same file, it is being uploaded to the Google Drive and the message alerting the user that it is uploaded is being displayed. I know there is a problem with my boolean but I am not too sure how to resolve it.
Sorry for the lengthy post. It is just this last hurdle and I want to understand how can I sort of check the condition whether its true or not because if I understand correctly, in the last return true from this
Compare Hash Function:
public static bool compareHash(string path)
{
DriveService service = GetService();
FilesResource.ListRequest FileListRequest = service.Files.List();
IList<Google.Apis.Drive.v3.Data.File> files = FileListRequest.Execute().Files;
List<GoogleDriveFiles> FileList = new List<GoogleDriveFiles>();
foreach (var file in files)
{
if (file.Equals(path))
{
return false; //if it is a duplicate return false
}
else
{
return true; //if it is new, then return true
}
}
return false; //I am not too sure what this affects the overall boolean of the code
}
File Upload Method:
public static bool 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);
if(compareHash(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 FileStream(path, FileMode.Open))
{
request = service.Files.Create(FileMetaData, stream, FileMetaData.MimeType);
request.Fields = "id";
request.Upload();
}
return true;
}
return false;
}
return true;//it will always return true to the controller for upload, yes? How do i make it like
//check for the condition from the hash generator?
}
C#:
[HttpPost]
public JsonResult UploadFile(HttpPostedFileBase file)
{
bool kk = GoogleDriveFilesRepository.FileUpload(file);
return Json(kk, JsonRequestBehavior.AllowGet);
}
File Upload Razor:
event.preventDefault();
var formData = new FormData($('form')[0]);
$.ajax({
url: "/Home/UploadFile",
type: 'Post',
success: function (result) {
if (result) {
Swal.fire({
title: 'Wait awhile...',
text: 'File will be uploaded shortly',
type: 'success',
confirmButtonText: 'Okay, cool',
timer: 4000
})
location.href = "/Home/GetGoogleDriveFiles";
} else {
Swal.fire({
title: 'OOps',
text: 'File unable to be uploaded shortly',
type: 'error',
confirmButtonText: 'Okay, then',
timer: 4000
})
location.href = "/Home/GetGoogleDriveFiles";
}
},
data: formData,
cache: false,
contentType: false,
processData: false
});
Sorry for the lengthy post. It is just this last hurdle and I want to understand how can I sort of check the condition whether its true or not because if I understand correctly, in the last return true from this
return true;//it will always return true to the controller for upload, yes? How do i make it like
//check for the condition from the hash generator?
overrides the entire boolean and jsut return true all the time.