public ActionResult UploadNewFiles(FineUpload upload, string extraParam1, int?extraParam2) { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("Index", "Home")); } var owner = ((MyMembershipProvider)Membership.Provider).GetUserByEmail(User.Identity.Name); if (owner == null) { return(RedirectToAction("Index", "Home")); } var fileName = Path.GetFileNameWithoutExtension(upload.FileName); var fileExtension = Path.GetExtension(upload.FileName); if (string.IsNullOrWhiteSpace(fileName) || string.IsNullOrWhiteSpace(fileExtension)) { return(new FineUploaderResult(false, error: "The file or file name is empty!")); } var fullName = fileName.ToHashedString() + fileExtension; var fileSectionNumber = int.Parse(upload.FileSection); var ipAdress = ConfigurationManager.AppSettings["Server"]; var filePath = string.Format(@"d:\UploadFiles\{0}\{1}", fileSectionNumber, fullName); //var pathToSave = Path.Combine(ipAdress, filePath); var pathToSave = string.Format(@"{0}\{1}", ipAdress, filePath); if (System.IO.File.Exists(pathToSave)) { return(new FineUploaderResult(false, error: "The same file is already exists!")); } var fileSize = upload.InputStream.Length; upload.SaveAs(pathToSave); var fileSection = _homeService.GetFileSectionById(fileSectionNumber); try { _fileService.AddFile(fileName, fullName, fileSize, filePath, ipAdress, fileSection, owner); } catch (DataException ex) { return(new FineUploaderResult(false, error: ex.Message)); } return(new FineUploaderResult(true, new { message = "Upload is finished successfully!" })); }
public async Task <IActionResult> Upload(FineUpload upload) { if (upload != null) { try { await upload.SaveAs(true); } catch (Exception ex) { return(new FineUploaderResult(false, error: ex.Message)); } return(new FineUploaderResult(true)); } return(new FineUploaderResult(false)); }
public FineUploaderResult SnimiSlikuElementa(FineUpload upload, short tipDokumenta, string postedFileName, string idElementa) { var korisnik = RuntimeDataHelpers.GetRuntimeData(Request); Directory.CreateDirectory(Server.MapPath("~/Content/Okruzi/")); var filePath = Path.Combine(Server.MapPath("~/Content/Okruzi/"), idElementa + ".jpeg"); try { upload.SaveAs(filePath, true); ImageHelper.SrediSliku(filePath, 1200, 100, false); } catch (Exception ex) { return(new FineUploaderResult(false, error: ExceptionParser.Parsiraj(korisnik, ex))); } return(new FineUploaderResult(true, new { url = Url.Content("~/Content/Okruzi/" + idElementa + ".jpeg?" + Guid.NewGuid().ToString()) })); }
public FineUploaderResult PostUploadFile(FineUpload upload) { // asp.net mvc will set extraParam1 and extraParam2 from the params object passed by Fine-Uploader var dir = @"c:\temp"; var filePath = Path.Combine(dir, upload.Filename); try { upload.SaveAs(filePath); } catch (Exception ex) { return(new FineUploaderResult(false, error: ex.Message)); } // the anonymous object in the result below will be convert to json and set back to the browser return(new FineUploaderResult(true, new { extraInformation = 12345 })); }
public FineUploaderResult CreateBrandWithImage(FineUpload upload, string BrandName) { try { Brand b = new Brand(); b.BrandName = BrandName; b.BrandImage = upload.Filename; db.Brands.Add(b); db.SaveChanges(); var dir = Server.MapPath("../Content/brandImages/" + b.BrandId + "/"); var filePath = Path.Combine(dir, upload.Filename); upload.SaveAs(filePath); return(new FineUploaderResult(true, new { FilePath = filePath })); } catch (Exception ex) { return(new FineUploaderResult(false, error: ex.Message)); } }
public FineUploaderResult CreateProductImages(FineUpload upload, int ProductId) { try { ProductImage p = new ProductImage(); p.ProductId = ProductId; p.Image = upload.Filename; db.ProductImages.Add(p); db.SaveChanges(); var dir = Server.MapPath("../Content/ProductImages/" + p.ProductId + "/"); var filePath = Path.Combine(dir, upload.Filename); upload.SaveAs(filePath); return(new FineUploaderResult(true, new { FilePath = filePath })); } catch (Exception ex) { return(new FineUploaderResult(false, error: ex.Message)); } }
public FineUploaderResult UploadFile(FineUpload upload) { var dir = Server.MapPath("~/Upload"); int pos1 = upload.Filename.LastIndexOf("\\"); string fileName = pos1 == -1 ? upload.Filename : upload.Filename.Substring(pos1 + 1, upload.Filename.Length - pos1 - 1); string storeName = Guid.NewGuid() + fileName; var filePath = Path.Combine(dir, storeName); try { upload.SaveAs(filePath); ViewBag.SourcePath = filePath; } catch (Exception ex) { return(new FineUploaderResult(false, error: ex.Message)); } // the anonymous object in the result below will be convert to json and set back to the browser return(new FineUploaderResult(true, new { extraInformation = filePath })); }
public FineUploaderResult UpdateBrandWithImage(FineUpload upload, int BrandId, string BrandName) { try { Brand b = db.Brands.FirstOrDefault(x => x.BrandId == BrandId); var currentFileName = b.BrandImage; b.BrandName = BrandName; b.BrandImage = upload.Filename; db.SaveChanges(); var dir = Server.MapPath("../Content/brandImages/" + b.BrandId + "/"); var filePath = Path.Combine(dir, upload.Filename); upload.SaveAs(filePath); var currentFilePath = Path.Combine(dir, currentFileName); System.IO.File.Delete(currentFilePath); return(new FineUploaderResult(true, new { FilePath = filePath })); } catch (Exception ex) { return(new FineUploaderResult(false, error: ex.Message)); } }
public FineUploaderResult UpdateFeatureImage(FineUpload upload, int ProductId) { try { Product p = db.Products.SingleOrDefault(x => x.ProductId == ProductId); var currentFileName = p.ProductFeatureImage; p.ProductFeatureImage = upload.Filename; db.Entry(p).State = EntityState.Modified; db.SaveChanges(); var dir = Server.MapPath("../Content/ProductImages/" + p.ProductId + "/"); var filePath = Path.Combine(dir, upload.Filename); upload.SaveAs(filePath); var currentFilePath = Path.Combine(dir, currentFileName); System.IO.File.Delete(currentFilePath); return(new FineUploaderResult(true, new { FilePath = filePath })); } catch (Exception ex) { return(new FineUploaderResult(false, error: ex.Message)); } }
public FineUploaderResult UploadDokumenta(FineUpload upload, long idPredmeta) { var korisnik = RuntimeDataHelpers.GetRuntimeData(Request); PodaciDokumenta fajl; try { Directory.CreateDirectory(Server.MapPath("~/App_Data/")); var replacedFileName = upload.Filename.Replace("&", "_"); string filePath = Path.Combine(Server.MapPath("~/App_Data/"), replacedFileName); upload.SaveAs(filePath); fajl = DMSData.SnimiDokumentPredmeta(korisnik, idPredmeta, upload.Filename, filePath); } catch (Exception ex) { return(new FineUploaderResult(false, error: ExceptionParser.Parsiraj(korisnik, ex))); } return(new FineUploaderResult(true, new { file = fajl })); }
private FineUploaderResult SaveUploadedFile(FineUpload upload, Guid?EntityID, int?EntityType, out string relativeFilePath) { string entityType = string.Empty; switch (EntityType) { case 1: entityType = "Person"; break; case 2: entityType = "Org"; break; case 3: entityType = "Dog"; break; } string folder = Path.Combine("~/Upload", entityType, EntityID.Value.ToString(), "Files"); string directory = Server.MapPath(folder); var fullFilePath = Path.Combine(directory, HttpContext.Server.UrlDecode(upload.Filename)); relativeFilePath = Path.Combine(folder, HttpContext.Server.UrlDecode(upload.Filename)); FineUploaderResult uploadResult = new FineUploaderResult(true); try { upload.SaveAs(fullFilePath); } catch (Exception ex) { uploadResult = new FineUploaderResult(false, error: ex.Message); } return(uploadResult); }
public ActionResult ProcessUpload(FineUpload upload) { string saveUrl = "/Uploads/"; string dirPath = _environment.WebRootPath + saveUrl; string fileExt = Path.GetExtension(upload.Filename).ToLower(); string newFileName = Guid.NewGuid().ToString("N") + fileExt; if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } var filePath = Path.Combine(dirPath, newFileName); try { upload.SaveAs(filePath); } catch (Exception ex) { return(Json(new { success = false, Url = "/Uploads/" + newFileName, isUploaded = false, message = "上传失败" })); } return(Json(new { success = true, Url = "/Uploads/" + newFileName, isUploaded = true, message = "上传成功" })); }
public virtual FineUploaderResult UploadFile(FineUpload upload, int id, string propName) { var settings = GetFileSettings(propName); if (upload.InputStream != null && upload.InputStream.Length != 0 && settings != null) { try { var entity = Repository.GetByPrimaryKey(id); var value = TypeHelpers.GetPropertyValue(entity, propName); Func <IFileEntity> saveImage = () => { // надо создать новое value и добавить его в базу var t = TypeHelpers.GetPropertyType(entity, propName); var file = (IFileEntity)Activator.CreateInstance(t); // формируем пути var defaultPath = AppConfig.GetValue(AppConfig.BasePathForImages); if (String.IsNullOrEmpty(defaultPath)) { defaultPath = "~/Content/"; } var relativePath = settings.PathToSave ?? defaultPath; if (string.IsNullOrEmpty(relativePath)) { throw new Exception("Не указан путь сохранения файла"); } var path = Server.MapPath(relativePath); var filename = string.Format("{0}{1}", Guid.NewGuid(), Path.GetExtension(upload.Filename)); var fullname = Path.Combine(path, filename); // сохраняем файл if (settings.IsImage) { DefaultFileService.ResaveImage(upload, fullname, settings); } else { upload.SaveAs(fullname); } file.Name = Path.Combine(relativePath, filename); file.SourceName = upload.Filename; file.Date = DateTime.Now; file.Visibility = true; file.Size = new FileInfo(fullname).Length; var entitySetName = TypeHelpers.GetEntitySetName(file, Repository.DataContext); ((IObjectContextAdapter)Repository.DataContext).ObjectContext.AddObject(entitySetName, file); return(file); }; IFileEntity result; if (value is IEnumerable) { result = saveImage(); var type = value.GetType(); MethodInfo methodInfo = type.GetMethod("Add"); object[] parametersArray = new object[] { result }; methodInfo.Invoke(value, parametersArray); // aganzha //var set = (System.Collections.Generic.HashSet<object>)value; //var type = result.GetType(); //var list = ((IListSource)value).GetList(); //var sortList = list.Cast<IFileEntity>().ToList(); //if (sortList.Any()) //{ // result.Sort = sortList.Max(fileEntity => fileEntity.Sort) + 1; //} //list.Add(result); } else { result = (IFileEntity)value; if (result != null) { DefaultFileService.DeleteFile(result, ControllerContext.HttpContext); ((IObjectContextAdapter)Repository.DataContext).ObjectContext.DeleteObject(result); } result = saveImage(); TypeHelpers.SetPropertyValue(entity, propName, result); } Repository.Save(); return(new FineUploaderResult(true, new { result.Id, Url = Url.Content(result.Name), result.Alt, result.Title, result.Description, result.Visibility, result.SourceName })); } catch (Exception exception) { return(new FineUploaderResult(false, null, exception.Message)); } } return(new FineUploaderResult(false, null, "файл не был передан")); }
public FineUploaderResult UploadFile(FineUpload upload, ImageUploadViewModel model, FormCollection form) { int counter = 1; // var hardPath = Server.MapPath("~/").Replace("httpdocs\\Presentation\\Nop.Web\\", "uploaded_image_libraries\\"); var hardPath = Path.Combine(Server.MapPath("~/App_Data/"), ImageLibrary); if (!Directory.Exists(hardPath)) { try { Directory.CreateDirectory(hardPath); } catch (Exception) { throw new Exception("Cannot Create BaseLine Config."); } } var currentShow = Path.Combine(hardPath, CurrentShow); // var dir = @"c:\upload\path"; // var filePath = Path.Combine(dir, upload.Filename); if (!Directory.Exists(currentShow)) { try { Directory.CreateDirectory(currentShow); } catch (Exception) { throw new Exception("Cannot Create BaseLine Show File"); } } var uploadFileName = Path.GetFileName(upload.Filename); if (uploadFileName == null) { return(new FineUploaderResult(false, error: "BadInput")); } var owner = model.EmailAddress.Replace("@", "."); var xmlFileName = model.ArtTitle + ".xml"; // var dir = Server.MapPath(currentShow) + owner; // var dir = OffsetFromBase + owner; var dir = currentShow + "\\" + owner; var filePath = Path.Combine(dir, uploadFileName); var infoPath = Path.Combine(dir, xmlFileName); string xmlFile = BuildXmlFile(model, filePath); if (Directory.Exists(dir)) { var directoryFiles = Directory.GetFiles(dir); counter += directoryFiles.Count(path => Path.GetExtension(path).ToLower() == ".xml"); } if (counter <= MaxCount) { try { if (System.IO.File.Exists(filePath)) { throw new Exception("This image previously submitted!"); } upload.SaveAs(filePath); } catch (Exception ex) { return(new FineUploaderResult(false, error: ex.Message)); } try { if (System.IO.File.Exists(infoPath)) { throw new Exception("This image previously submitted!"); } System.IO.File.WriteAllText(infoPath, xmlFile); } catch (Exception ex) { // remove uploaded file if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); } return(new FineUploaderResult(false, error: ex.Message)); } model.ModelState = "Uploaded"; } // var jresult = (counter >= MaxCount) ? "Limit Reached, Pay Fee Below" : "Submit New Entry or Pay Fee Below"; var jresult = (counter >= MaxCount)? "Limit Reached - Submitted Count: " + counter : "Submit New Entry - Submitted Count: " + counter; // the anonymous object in the result below will be convert to json and set back to the browser // return values bool success, object otherData = null, string error = null, bool? preventRetry = null return(new FineUploaderResult(true, new { extraInformation = jresult })); }