public ActionResult Create(Medicine medicine, HttpPostedFileBase file) { try { IBL bl = new BL.BL(); if (file == null) { bl.AddMedicine(medicine); return(RedirectToAction("Index")); } GoogleDriveAPIHelper googleDrive = new GoogleDriveAPIHelper(); googleDrive.UploadFileOnDrive(file); medicine.ImagePath = googleDrive.DownloadGoogleFileByName(file.FileName); ImageValidate imageValidate = new ImageValidate(); bool result = true; if (medicine.ImagePath != null) { result = imageValidate.Validate(medicine.ImagePath); } if (result) { medicine.ImagePath = $"/GoogleDriveFiles/{file.FileName}"; //add image to drive bl.AddMedicine(medicine); } else { Session["Error"] = "This image isn't valid"; return(View("AddImage", new MedicineViewModel(medicine))); } return(RedirectToAction("Index")); }catch (Exception e) { return(View()); } }
public ActionResult Index(HttpPostedFileBase file) { if (file != null) { GoogleDriveAPIHelper.UplaodFileOnDrive(file); ViewBag.Success = "File Uploaded on Google Drive"; return(View()); } ViewBag.Failed = "something went wrong."; return(View()); }
//This method is required by Google Drive API public ActionResult IndexAsync(CancellationToken cancellationToken) { AuthorizationCodeWebApp.AuthResult authorization = GoogleDriveAPIHelper.getAuthorization(this); if (authorization.Credential == null) { return(new RedirectResult(authorization.RedirectUri)); } else { return(RedirectToAction(nameof(Index))); } }
public void del(string file) { DAL.GoogleDriveAPIHelper g = new GoogleDriveAPIHelper(); List <GoogleDriveFile> u = g.GetDriveFiles(); foreach (GoogleDriveFile f in u) { if (f.Name == file) { g.DeleteFile(f); } } }
public ActionResult Edit([Bind(Include = "ItemTypeId,Name,Image,CategoryId")] ItemTypes itemTypes, HttpPostedFileBase file) { if (ModelState.IsValid) { //jpg and png only accepted byte[] bytesRead = new byte[8]; if (file == null) { ModelState.AddModelError("", "Image is Required"); } else { file.InputStream.Read(bytesRead, 0, 8); if ((bytesRead[0] == 255 && bytesRead[1] == 216) || (bytesRead[0] == 137 && bytesRead[1] == 80 && bytesRead[2] == 78 && bytesRead[3] == 71 && bytesRead[4] == 13 && bytesRead[5] == 10 && bytesRead[6] == 26 && bytesRead[7] == 10)) { //create service var service = GoogleDriveAPIHelper.GetService(); var FileMetaData = new Google.Apis.Drive.v3.Data.File(); FileMetaData.Name = Path.GetFileName(file.FileName); FileMetaData.MimeType = file.ContentType; FilesResource.CreateMediaUpload request; using (var stream = file.InputStream) { request = service.Files.Create(FileMetaData, stream, FileMetaData.MimeType); request.Fields = "id"; // var fileC = request.ResponseBody; request.Upload(); } var filei = request.ResponseBody; itemTypes.Image = "https://drive.google.com/uc?id=" + filei.Id; //itemTypes.ItemTypeId = 1; var cat = db.Categories.FirstOrDefault(x => x.CategoryId == itemTypes.CategoryId); itemTypes.Categories = cat; db.Entry(itemTypes).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } ModelState.AddModelError("", "Invalid image"); } } ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "CategoryName", itemTypes.CategoryId); return(View(itemTypes)); }
public ActionResult ExportGdrive(string content) { using (MemoryStream stream = new System.IO.MemoryStream()) { StringReader sr = new StringReader(content); Document pdfDoc = new Document(PageSize.A4, 30f, 20f, 30f, 20f); PdfWriter writer = PdfWriter.GetInstance(pdfDoc, new FileStream(Server.MapPath("~/GDrive/Report.pdf"), FileMode.Create)); //PdfWriter.GetInstance(pdfDoc, stream); pdfDoc.Open(); XMLWorkerHelper.GetInstance().ParseXHtml(writer, pdfDoc, sr); pdfDoc.Close(); FileStream file = System.IO.File.Open(Server.MapPath("~/GDrive/Report.pdf"), FileMode.Open); //System.IO.File.Open( ("~/GDrive/Report.pdf"); // Upload file to root folder //GoogleDriveAPIHelper.UplaodFileOnDrive(file); // Upload file to specific folder GoogleDriveAPIHelper.FileUploadInFolder("1bEuGDeH8jKmBsOvwJl0AVKfalF0OHlPR", file); ViewBag.Success = "File Uploaded on Google Drive."; return(View()); } }
public void Uplaod(HttpPostedFileBase file) { DAL.GoogleDriveAPIHelper g = new GoogleDriveAPIHelper(); g.UplaodFileOnDrive(file); }
public ActionResult Index(HttpPostedFileBase file) { GoogleDriveAPIHelper.UplaodFileOnDrive(file); ViewBag.Success = "File Uploaded on Google Drive"; return(View()); }
public ActionResult Index(HttpPostedFileBase[] file, string folderName, string cate) { //GoogleDriveAPIHelper.CreateFolder(folderName); string id = GoogleDriveAPIHelper.GetLinkFolder(folderName); if (id == "") { GoogleDriveAPIHelper.CreateFolder(folderName); id = GoogleDriveAPIHelper.GetLinkFolder(folderName); } //GoogleDriveAPIHelper.UplaodFileOnDrive(file); foreach (var item in file) { try { //GoogleDriveAPIHelper.UplaodFileOnDrive(item); GoogleDriveAPIHelper.FileUploadInFolder(id, item); } catch { ViewBag.Success = "File Upload fail on Google Drive"; ViewBag.listLink = new List <string>(); ViewBag.fileName = new List <string>(); return(View()); } } var lnk = GoogleDriveAPIHelper.linkDrive; var fileName = GoogleDriveAPIHelper.fileName; ViewBag.Success = "File Uploaded on Google Drive"; //List<String> list_link = new List<String>(); //foreach (var item in lnk) //{ // string[] temp = item.ToString().Split(' '); // list_link.Add(temp[0]); //} //ViewBag.list_link = list_link; ViewBag.listLink = lnk; ViewBag.fileName = fileName; GoogleDriveAPIHelper.DeleteFolder(); int count = lnk.Count(); //ViewBag.count = count;sau đó thêm sản phẩm vào danh mục ý // Ý tưởng viết tiếp //cho chọn danh mục for (int i = 0; i < count; i++) { tbl_file new_file = new tbl_file(); new_file.file_name = fileName[i]; new_file.id_cate = Convert.ToInt32(cate); new_file.link_drive = lnk[i]; new_file.status = 0; db.tbl_file.Add(new_file); db.SaveChanges(); } var category = db.tbl_category.ToList(); ViewBag.category = category; return(View()); }
public ActionResult Create(string submitButton, Guid?ParentId, HttpPostedFileBase File, Guid?FileId, string FileDescription, bool?IsGlobalFile, Guid?DirectoryId, string DirectoryName, bool?IsGlobalDirectory) { FilesModel model = new FilesModel(); if (ParentId == null) { model.ParentId = RootId; } else { model.ParentId = (Guid)ParentId; } if (IsGlobalFile == true || IsGlobalDirectory == true) { model.Branches_Id = null; } else { model.Branches_Id = Helper.getActiveBranchId(Session); } if (submitButton == BUTTONVALUE_UPLOADFILE || submitButton == BUTTONVALUE_UPLOADREVISION) { if (File == null || File.ContentLength == 0) { UtilWebMVC.setBootboxMessage(this, "Invalid File"); } else { AuthorizationCodeWebApp.AuthResult authorization = GoogleDriveAPIHelper.getAuthorization(this); if (authorization.Credential == null) { UtilWebMVC.setBootboxMessage(this, "Google Login was required. Please navigate to the folder and try to upload the file again."); return(RedirectToAction(nameof(IndexAsync))); } else { authorization = GoogleDriveAPIHelper.checkExpiration(this, authorization); string OnlineFileId = GoogleDriveAPIHelper.UploadToDrive(GoogleDriveAPIHelper.GetServiceForWebApplication(authorization), File); if (GoogleDriveAPIHelper.isFileUploadSuccessful(this, OnlineFileId)) { model.Filename = File.FileName; model.OnlineFileId = OnlineFileId; model.Notes = FileDescription; if (submitButton == BUTTONVALUE_UPLOADFILE) { add(model); } else { FilesModel originalModel = get((Guid)FileId); model.ParentId = originalModel.ParentId; add(model); originalModel.ParentId = model.Id; update(originalModel); } } } } } else if (submitButton == BUTTONVALUE_CREATEDIRECTORY || submitButton == BUTTONVALUE_UPDATEDIRECTORY) { if (string.IsNullOrWhiteSpace(DirectoryName)) { UtilWebMVC.setBootboxMessage(this, "Invalid Name"); } else { model.DirectoryName = DirectoryName; if (submitButton == BUTTONVALUE_CREATEDIRECTORY) { add(model); } else { model.Id = (Guid)DirectoryId; update(model); } } } return(null); }