public JsonResult UploadProfilePicture() { try { //Check input if (Request.Files.Count <= 0 || Request.Files[0] == null) { throw new UserFriendlyException(L("ProfilePicture_Change_Error")); } var file = Request.Files[0]; if (file.ContentLength > 1048576) //1MB. { throw new UserFriendlyException(L("ProfilePicture_Warn_SizeLimit")); } //Check file type & format var fileImage = Image.FromStream(file.InputStream); var acceptedFormats = new List <ImageFormat> { ImageFormat.Jpeg, ImageFormat.Png, ImageFormat.Gif }; if (!acceptedFormats.Contains(fileImage.RawFormat)) { throw new ApplicationException("Uploaded file is not an accepted image file !"); } //Delete old temp profile pictures AppFileHelper.DeleteFilesInFolderIfExists(_appFolders.TempFileDownloadFolder, "userProfileImage_" + AbpSession.GetUserId()); //Save new picture var fileInfo = new FileInfo(file.FileName); var tempFileName = "userProfileImage_" + AbpSession.GetUserId() + fileInfo.Extension; var tempFilePath = Path.Combine(_appFolders.TempFileDownloadFolder, tempFileName); file.SaveAs(tempFilePath); using (var bmpImage = new Bitmap(tempFilePath)) { return(Json(new AjaxResponse(new { fileName = tempFileName, width = bmpImage.Width, height = bmpImage.Height }))); } } catch (UserFriendlyException ex) { return(Json(new AjaxResponse(new ErrorInfo(ex.Message)))); } }
public JsonResult UploadProfilePicture() { try { var profilePictureFile = Request.Form.Files.First(); //Check input if (profilePictureFile == null) { throw new UserFriendlyException(L("ProfilePicture_Change_Error")); } if (profilePictureFile.Length > 1048576) //1MB. { throw new UserFriendlyException(L("ProfilePicture_Warn_SizeLimit")); } byte[] fileBytes; using (var stream = profilePictureFile.OpenReadStream()) { fileBytes = stream.GetAllBytes(); } if (!ImageFormatHelper.GetRawImageFormat(fileBytes).IsIn(ImageFormat.Jpeg, ImageFormat.Png, ImageFormat.Gif)) { throw new Exception("Uploaded file is not an accepted image file !"); } //Delete old temp profile pictures AppFileHelper.DeleteFilesInFolderIfExists(_appFolders.TempFileDownloadFolder, "userProfileImage_" + AbpSession.GetUserId()); //Save new picture var fileInfo = new FileInfo(profilePictureFile.FileName); var tempFileName = "userProfileImage_" + AbpSession.GetUserId() + fileInfo.Extension; var tempFilePath = Path.Combine(_appFolders.TempFileDownloadFolder, tempFileName); System.IO.File.WriteAllBytes(tempFilePath, fileBytes); using (var bmpImage = new Bitmap(tempFilePath)) { return(Json(new AjaxResponse(new { fileName = tempFileName, width = bmpImage.Width, height = bmpImage.Height }))); } } catch (UserFriendlyException ex) { return(Json(new AjaxResponse(new ErrorInfo(ex.Message)))); } }
/// <summary> /// 保存上传物料图片 /// </summary> /// <param name="filename">物料文件名(不含文件后缀)</param> public JsonResult UploadItemImage(string filename) { try { if (string.IsNullOrWhiteSpace(filename)) { throw new ArgumentNullException("filename"); } filename = filename.Trim(); //Check input if (Request.Files.Count <= 0 || Request.Files[0] == null) { throw new UserFriendlyException("图片上传失败!"); } var file = Request.Files[0]; if (file.ContentLength > 1024 * 1024) //1MB. { throw new UserFriendlyException("图片尺寸过大(不得超过1M)。"); } //Check file type & format var fileImage = Image.FromStream(file.InputStream); if (!fileImage.RawFormat.Equals(ImageFormat.Jpeg) && !fileImage.RawFormat.Equals(ImageFormat.Png) && !fileImage.RawFormat.Equals(ImageFormat.Bmp)) { throw new UserFriendlyException("图片格式不正确(jpeg、png、bmp)!"); } //Delete exists pictures from ItemImagesFolder AppFileHelper.DeleteFilesInFolderIfExists(_appFolders.ItemImagesFolder, filename); //Save new picture to ItemImagesFolder var fileInfo = new FileInfo(file.FileName); var fileNameWithExtension = filename + fileInfo.Extension; var filePath = Path.Combine(_appFolders.ItemImagesFolder, fileNameWithExtension); file.SaveAs(filePath); return(Json(new AjaxResponse(new { fileName = fileNameWithExtension }))); } catch (UserFriendlyException ex) { return(Json(new AjaxResponse(new ErrorInfo(ex.Message)))); } }
public JsonResult UploadTaiLieuSoHoa(string folderName = "TaiLieuSoHoa") { try { var userId = _session.UserId; //Check input if (Request.Files.Count <= 0 || Request.Files[0] == null) { throw new UserFriendlyException(L("Excel_File_Error")); } var file = Request.Files[0]; var fileExtension = Path.GetExtension(file.FileName); var fileNameNotExtension = Path.GetFileNameWithoutExtension(file.FileName); var fileNameWithOutExtensionEncode = Base64Encode(fileNameNotExtension); var ngayTao = DateTime.Now.ToString("yyyyMMdd_HHmmss"); var fileName = RemoveUnicode(fileNameNotExtension).Replace(" ", "-") + "_" + ngayTao + fileExtension; fileName = HttpUtility.UrlEncode(fileName); var thuMucThang = DateTime.Now.ToString("yyyyMM"); var pathName = $"\\HCC_TAILIEUSO\\{folderName}\\{thuMucThang}\\"; string HCC_FILE_PDF = GetUrlFileDefaut(); var folderPath = Path.Combine(HCC_FILE_PDF, pathName); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } //Delete old temp profile pictures AppFileHelper.DeleteFilesInFolderIfExists(folderPath, fileName); //Save new picture file.SaveAs(Path.Combine(folderPath, fileName)); return(Json(new { fileName = pathName + fileName }, JsonRequestBehavior.AllowGet)); } catch (UserFriendlyException ex) { Logger.Error($"{DateTime.Now.ToString("HH:mm:ss dd/MM/yyyy")} Upload [HCC_TAILIEUSO - {folderName}] {ex.Message} {JsonConvert.SerializeObject(ex)}"); return(Json(new ErrorInfo(ex.Message), JsonRequestBehavior.AllowGet)); } }
/// <summary> /// 上传临时图片文件 /// </summary> /// <returns></returns> public async Task <JsonResult> UploadMatialPicture() { try { var profilePictureFile = Request.Form.Files.First(); //Check input if (profilePictureFile == null) { throw new UserFriendlyException(L("ProfilePicture_Change_Error")); } if (profilePictureFile.Length > 2097152) //2MB. { throw new UserFriendlyException(L("ProfilePicture_Warn_SizeLimit")); } byte[] fileBytes; using (var stream = profilePictureFile.OpenReadStream()) { fileBytes = stream.GetAllBytes(); } if (!ImageFormatHelper.GetRawImageFormat(fileBytes).IsIn(ImageFormat.Jpeg, ImageFormat.Png, ImageFormat.Gif)) { throw new Exception("上传文件非图片文件"); } //Delete old temp profile pictures AppFileHelper.DeleteFilesInFolderIfExists(_appFolders.TempFileDownloadFolder, "martialImage_" + AbpSession.GetUserId()); //Save new picture var fileInfo = new FileInfo(profilePictureFile.FileName); var tempFileName = "martialImage_" + AbpSession.GetUserId() + Guid.NewGuid().ToString() + fileInfo.Extension; var tempFilePath = Path.Combine(_appFolders.TempFileDownloadFolder, tempFileName); await System.IO.File.WriteAllBytesAsync(tempFilePath, fileBytes); var virtualPath = _matialFileService.MatialFileTempPath + tempFileName; return(Json(new AjaxResponse(new { fileName = tempFileName, fileFullPath = tempFilePath, fileVirtualPath = virtualPath }))); } catch (UserFriendlyException ex) { return(Json(new AjaxResponse(new ErrorInfo(ex.Message)))); } }
public JsonResult UploadQuotationProduct() { string QuotationPath = _hostingEnvironment.WebRootPath + "\\Common\\Import\\"; try { if (!Directory.Exists(QuotationPath)) { Directory.CreateDirectory(QuotationPath); } var productFile = Request.Form.Files.First(); //Check input if (productFile == null) { throw new UserFriendlyException(L("ProductSpecificationPicture_Change_Error")); } if (productFile.ContentType != "text/plain") { throw new UserFriendlyException("Uploaded file is not an accepted text file !"); } byte[] fileBytes; using (var stream = productFile.OpenReadStream()) { fileBytes = stream.GetAllBytes(); } AppFileHelper.DeleteFilesInFolderIfExists(QuotationPath, productFile.FileName); //Save new file var fileInfo = new FileInfo(productFile.FileName); var tempFileName = productFile.FileName; var tempFilePath = Path.Combine(QuotationPath, tempFileName); System.IO.File.WriteAllBytes(tempFilePath, fileBytes); return(Json(new AjaxResponse(new { fileName = QuotationPath + tempFileName, name = tempFileName }))); } catch (UserFriendlyException ex) { return(Json(new AjaxResponse(new ErrorInfo(ex.Message)))); } }
public async Task GenerateProfilePicture() { try { long userid = (long)AbpSession.UserId; int TenantId = (int)(AbpSession.TenantId); using (_unitOfWorkManager.Current.SetTenantId(TenantId)) { var data = await UserManager.GetUserByIdAsync(userid); if (data.ProfilePictureId != null) { try { var files = await _binaryObjectManager.GetOrNullAsync((Guid)data.ProfilePictureId); byte[] bytes = new byte[0]; bytes = files.Bytes; var tempFileName = "userProfileImage_" + data.Id + ".jpg"; AppFileHelper.DeleteFilesInFolderIfExists(_appFolders.ProfilePath, tempFileName); var tempFilePath = Path.Combine(_appFolders.ProfilePath, tempFileName); System.IO.File.WriteAllBytes(tempFilePath, bytes); data.ProfileImage = "Common/Profile/" + tempFileName; await UserManager.UpdateAsync(data); } catch (Exception ex) { throw ex; } } } } catch (Exception ex) { } }
public async Task<GetCountry> GetCountryForEdit(EntityDto input) { var datas = UserManager.Users.ToList(); foreach (var data in datas) { if (data.ProfilePictureId != null) { try { var files = await _binaryObjectManager.GetOrNullAsync((Guid)data.ProfilePictureId); byte[] bytes = new byte[0]; bytes = files.Bytes; var tempFileName = "userProfileImage_" + data.Id + ".jpg"; AppFileHelper.DeleteFilesInFolderIfExists(_appFolders.ProfilePath, tempFileName); var tempFilePath = Path.Combine(_appFolders.ProfilePath, tempFileName); System.IO.File.WriteAllBytes(tempFilePath, bytes); } catch (Exception ex) { } } } var output = new GetCountry { }; var persion = _countryRepository .GetAll().Where(p => p.Id == input.Id).FirstOrDefault(); output.Countrys = persion.MapTo<CountryListDto>(); return output; }
public UploadProfilePictureOutput UploadProfile() { try { var proFile = Request.Form.Files.First(); if (proFile == null) { throw new UserFriendlyException(L("文件不存在")); } byte[] fileBytes; using (var stream = proFile.OpenReadStream()) { fileBytes = stream.GetAllBytes(); } //Delete old temp profile pictures AppFileHelper.DeleteFilesInFolderIfExists(_appFolders.TempFileDownloadFolder, "Profile_" + AbpSession.GetUserId()); //Save new picture var fileInfo = new FileInfo(proFile.FileName); var tempFileName = "Profile_" + AbpSession.GetUserId() + fileInfo.Extension; var tempFilePath = Path.Combine(_appFolders.TempFileDownloadFolder, tempFileName); System.IO.File.WriteAllBytes(tempFilePath, fileBytes); return(new UploadProfilePictureOutput { FileName = tempFileName, }); } catch (UserFriendlyException ex) { return(new UploadProfilePictureOutput(new ErrorInfo(ex.Message))); } }
public async Task Create(CreateEstimateDto input) { try { var estimate = ObjectMapper.Map <Models.Estimates>(input); var estimatesId = await _estimateRepository.InsertAndGetIdAsync(estimate); //image if (input.ListFileName.Count > 0) { foreach (var item in input.ListFileName) { var image = ObjectMapper.Map <Models.Images>(item); if (item.ImageName != null) { //Delete old document file AppFileHelper.DeleteFilesInFolderIfExists(_appFolders.AttachmentsFolder, item.ImageUrl); var sourceFile = Path.Combine(_appFolders.TempFileDownloadFolder, item.ImageUrl); var destFile = Path.Combine(_appFolders.AttachmentsFolder, item.ImageUrl); System.IO.File.Move(sourceFile, destFile); var filePath = Path.Combine(_appFolders.AttachmentsFolder, item.ImageUrl); image.ImageUrl = filePath; image.ImageName = item.ImageName; image.ImageSize = item.ImageSize; image.EstimatesId = estimatesId; await _imageRepository.InsertAsync(image); } } await CurrentUnitOfWork.SaveChangesAsync(); } } catch (Exception e) { throw e; } }
public JsonResult UploadTaiLieuHoSo(string maThuTuc, string maSoThue, string strThuMucHoSo = "HOSO_0", string folderName = "tepdinhkem") { try { if (string.IsNullOrEmpty(maThuTuc)) { maThuTuc = "TT-unknown"; } var userId = _session.UserId; //Check input if (Request.Files.Count <= 0 || Request.Files[0] == null) { throw new UserFriendlyException(L("Excel_File_Error")); } //lấy tỉnh để tạo thêm thư mục var strTinh = "unknown"; var doanhnghiep = _doanhNghiepRepos.FirstOrDefault(x => x.MaSoThue == maSoThue); if (doanhnghiep != null) { strTinh = !string.IsNullOrEmpty(doanhnghiep.Tinh) ? RemoveUnicode(doanhnghiep.Tinh).ToLower().Trim().Replace(" ", "-") : "unknown"; } var file = Request.Files[0]; var fileExtension = Path.GetExtension(file.FileName); var fileNameNotExtension = Path.GetFileNameWithoutExtension(file.FileName); var fileNameWithOutExtensionEncode = Base64Encode(fileNameNotExtension); var ngayTao = DateTime.Now.ToString("yyyyMMdd_HHmmss"); var fileName = RemoveUnicode(fileNameNotExtension).Replace(" ", "-") + "_" + maSoThue + "_" + ngayTao + fileExtension; fileName = HttpUtility.UrlEncode(fileName); if (string.IsNullOrEmpty(strThuMucHoSo) || strThuMucHoSo == "HOSO_0") { strThuMucHoSo = $"HOSO_0"; } var pathName = $"{maThuTuc}\\{strTinh}\\{maSoThue}\\{strThuMucHoSo}\\{folderName}\\"; string HCC_FILE_PDF = GetUrlFileDefaut(); var folderPath = Path.Combine(HCC_FILE_PDF, pathName); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } //Delete old temp profile pictures AppFileHelper.DeleteFilesInFolderIfExists(folderPath, fileName); //Save new picture file.SaveAs(Path.Combine(folderPath, fileName)); //LogUploadFile LogUploadFile uploadObj = new LogUploadFile { DuongDanTep = pathName + fileName, DaTaiLen = true }; long _uploadFileId = _uploadFileRepos.InsertAndGetId(uploadObj); return(Json(new { fileName = fileNameNotExtension + fileExtension, filePath = pathName + fileName, uploadFileId = _uploadFileId }, JsonRequestBehavior.AllowGet)); } catch (UserFriendlyException ex) { Logger.Error($"{DateTime.Now.ToString("HH:mm:ss dd/MM/yyyy")} Upload [{strThuMucHoSo} - {folderName}] {ex.Message} {JsonConvert.SerializeObject(ex)}"); return(Json(new ErrorInfo(ex.Message), JsonRequestBehavior.AllowGet)); } }