public async Task <ActionResult> UploadProfile()
        {
            var profile = Request.Form.Files.First();

            if (profile == null)
            {
                throw new Exception("Uploaded file is Empty !");
            }
            if (!Directory.Exists(_appFolders.TempFileDownloadFolder))
            {
                Directory.CreateDirectory(_appFolders.TempFileDownloadFolder);
            }
            byte[] fileBytes;
            using (var stream = profile.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(profile.FileName);
            var tempFileName = "userProfileImage_" + AbpSession.GetUserId() + fileInfo.Extension;
            var tempFilePath = Path.Combine(_appFolders.TempFileDownloadFolder, tempFileName);
            await System.IO.File.WriteAllBytesAsync(tempFilePath, fileBytes);

            return(new JsonResult(tempFileName));
        }
Exemple #2
0
        public UploadServiceImagesOutput UploadServiceImages(FileDto input)
        {
            try
            {
                var profilePictureFile = Request.Form.Files.First();

                byte[] fileBytes;
                using (var stream = profilePictureFile.OpenReadStream())
                {
                    fileBytes = stream.GetAllBytes();
                }

                if (!ImageFormatHelper.GetRawImageFormat(fileBytes).IsIn(ImageFormat.Jpeg, ImageFormat.Png, ImageFormat.Gif))
                {
                    throw new Exception(L("IncorrectImageFormat"));
                }
                _tempFileCacheManager.SetFile(input.FileToken, fileBytes);

                using (var bmpImage = new Bitmap(new MemoryStream(fileBytes)))
                {
                    return(new UploadServiceImagesOutput
                    {
                        FileToken = input.FileToken,
                        FileName = input.FileName,
                        FileType = input.FileType,
                        Width = bmpImage.Width,
                        Height = bmpImage.Height
                    });
                }
            }
            catch (UserFriendlyException ex)
            {
                return(new UploadServiceImagesOutput(new ErrorInfo(ex.Message)));
            }
        }
Exemple #3
0
        public async Task UploadAsync(long groupId)
        {
            var profilePictureFile = _contextAccessor.HttpContext.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 !");
            }

            await _pictureManager.UploadPictureAsync(fileBytes, profilePictureFile.FileName, groupId);
        }
Exemple #4
0
        public UploadProfilePictureOutput UploadProfilePicture()
        {
            try
            {
                var profilePictureFile = Request.Form.Files.First();

                //Check input
                if (profilePictureFile == null)
                {
                    throw new UserFriendlyException(L("ProfilePicture_Change_Error"));
                }

                if (profilePictureFile.Length > MaxProfilePictureSize)
                {
                    throw new UserFriendlyException(L("ProfilePicture_Warn_SizeLimit", AppConsts.MaxProfilPictureBytesUserFriendlyValue));
                }

                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);
                string ext      = fileInfo.Extension.ToLower().Trim();
                if (ext.Equals(".jpg") || ext.Equals(".png") || ext.Equals(".gif") || ext.Equals(".jpeg"))
                {
                    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(new UploadProfilePictureOutput
                        {
                            FileName = tempFileName,
                            Width = bmpImage.Width,
                            Height = bmpImage.Height
                        });
                    }
                }
                else
                {
                    throw new UserFriendlyException("Uploaded file format is not correct !");
                }
            }
            catch (Exception ex)
            {
                return(new UploadProfilePictureOutput(new ErrorInfo(ex.Message)));
            }
        }
Exemple #5
0
        /// <summary>
        /// 上传图片文件并上传至微信
        /// </summary>
        /// <returns></returns>
        public async Task <JsonResult> UploadMatialPic()
        {
            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, "martialPic_" + AbpSession.GetUserId());

                //Save new picture
                var fileInfo     = new FileInfo(profilePictureFile.FileName);
                var tempFileName = "martialPic_" + 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;


                var mediaId = "";
                try
                {
                    mediaId = await _wxMediaAppService.UploadMedia(tempFilePath, "");//上传至微信
                }
                catch (Exception e)
                {
                    Logger.Error("上传微信错误,错误信息:" + e.Message + ";错误堆栈:" + e.StackTrace);
                }

                //var mediaId = "测试";


                return(Json(new AjaxResponse(new { fileName = tempFileName, fileFullPath = tempFilePath, fileVirtualPath = virtualPath, mediaID = mediaId })));
            }
            catch (UserFriendlyException ex)
            {
                return(Json(new AjaxResponse(new ErrorInfo(ex.Message))));
            }
        }
Exemple #6
0
        public JsonResult UploadProductSubGroupPicture(int ProductSubGroupId, string ImgPath)
        {
            try
            {
                var productSubGroupPictureFile = Request.Form.Files.First();

                //Check input
                if (productSubGroupPictureFile == null)
                {
                    throw new UserFriendlyException(L("ProductSubGroupPicture_Change_Error"));
                }

                if (productSubGroupPictureFile.Length > 1048576) //1MB.
                {
                    throw new UserFriendlyException(L("ProductSubGroupPicture_Warn_SizeLimit"));
                }

                byte[] fileBytes;
                using (var stream = productSubGroupPictureFile.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 !");
                }

                var ProductSubGroup = _appFolders.ProductSubGroupFilePath;

                if (!Directory.Exists(ProductSubGroup))
                {
                    Directory.CreateDirectory(ProductSubGroup);
                }

                //Delete old ProductSubGroup pictures
                AppFileHelper.DeleteFilesInFolderIfExists(_appFolders.FindFilePath, ImgPath);

                //Save new picture
                var fileInfo     = new FileInfo(productSubGroupPictureFile.FileName);
                var tempFileName = productSubGroupPictureFile.FileName + fileInfo.Extension;
                var tempFilePath = Path.Combine(ProductSubGroup, tempFileName);
                System.IO.File.WriteAllBytes(tempFilePath, fileBytes);

                using (var bmpImage = new Bitmap(tempFilePath))
                {
                    return(Json(new AjaxResponse(new { fileName = "Common/Images/ProductSubGroup/" + tempFileName, width = bmpImage.Width, height = bmpImage.Height })));
                }
            }
            catch (UserFriendlyException ex)
            {
                return(Json(new AjaxResponse(new ErrorInfo(ex.Message))));
            }
        }
Exemple #7
0
        public JsonResult UploadMultiTempProductPicture(int TempProductId)
        {
            try
            {
                var TempProductPictureFile = Request.Form.Files.First();

                //Check input
                if (TempProductPictureFile == null)
                {
                    throw new UserFriendlyException(L("ProductPicture_Change_Error"));
                }

                if (TempProductPictureFile.Length > 1048576) //1MB.
                {
                    throw new UserFriendlyException(L("ProductPicture_Warn_SizeLimit"));
                }

                byte[] fileBytes;
                using (var stream = TempProductPictureFile.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 !");
                }
                var TempProducts = _appFolders.TempProductFilePath + @"\" + TempProductId;

                if (!Directory.Exists(TempProducts))
                {
                    Directory.CreateDirectory(TempProducts);
                }

                //Delete old temp product pictures
                AppFileHelper.DeleteFilesInFolderIfExists(TempProducts, TempProductPictureFile.FileName);

                //Save new picture
                var fileInfo     = new FileInfo(TempProductPictureFile.FileName);
                var tempFileName = TempProductPictureFile.FileName;
                var tempFilePath = Path.Combine(TempProducts, tempFileName);
                System.IO.File.WriteAllBytes(tempFilePath, fileBytes);

                using (var bmpImage = new Bitmap(tempFilePath))
                {
                    return(Json(new AjaxResponse(new { fileName = "Common/Images/TemporaryProduct/" + TempProductId + "/" + tempFileName, width = bmpImage.Width, height = bmpImage.Height })));
                }
            }
            catch (UserFriendlyException ex)
            {
                return(Json(new AjaxResponse(new ErrorInfo(ex.Message))));
            }
        }
Exemple #8
0
        public UploadProfilePictureOutputDto UploadProfilePicture(FileDto input)
        {
            try
            {
                var profilePictureFile = Request.Form.Files.First();

                //Check input
                if (profilePictureFile == null)
                {
                    throw new UserFriendlyException(L("ProfilePicture_Change_Error"));
                }

                //上传的图片超过大小限制
                if (profilePictureFile.Length > MaxProfilePictureSize)
                {
                    throw new UserFriendlyException(L("ProfilePicture_Warn_SizeLimit", AppConsts.MaxProfilePictureBytesUserFriendlyValue));
                }

                byte[] fileBytes;
                using (var stream = profilePictureFile.OpenReadStream())
                {
                    fileBytes = stream.GetAllBytes();
                }

                if (!ImageFormatHelper.GetRawImageFormat(fileBytes).IsIn(ImageFormat.Jpeg, ImageFormat.Png, ImageFormat.Gif))
                {
                    throw new Exception("请上传图片文件,仅接受Jpg、PNG、Gif三种格式!");
                }

                _dataTempFileCacheManager.SetFile(input.FileToken, fileBytes);

                using (var bmpImage = new Bitmap(new MemoryStream(fileBytes)))
                {
                    return(new UploadProfilePictureOutputDto
                    {
                        FileToken = input.FileToken,
                        FileName = input.FileName,
                        FileType = input.FileType,
                        Width = bmpImage.Width,
                        Height = bmpImage.Height
                    });
                }
            }
            catch (UserFriendlyException ex)
            {
                return(new UploadProfilePictureOutputDto(new ErrorInfo(ex.Message)));
            }
        }
Exemple #9
0
        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))));
            }
        }
Exemple #10
0
        public async Task <JsonResult> UploadLogo()
        {
            try
            {
                var logoFile = Request.Form.Files.First();

                //Check input
                if (logoFile == null)
                {
                    throw new UserFriendlyException(L("File_Empty_Error"));
                }

                if (logoFile.Length > 30720) //30KB
                {
                    throw new UserFriendlyException(L("File_SizeLimit_Error"));
                }

                byte[] fileBytes;
                using (var stream = logoFile.OpenReadStream())
                {
                    fileBytes = stream.GetAllBytes();
                }

                var imageFormat = ImageFormatHelper.GetRawImageFormat(fileBytes);
                if (!imageFormat.IsIn(ImageFormat.Jpeg, ImageFormat.Png, ImageFormat.Gif))
                {
                    throw new UserFriendlyException("File_Invalid_Type_Error");
                }

                var logoObject = new BinaryObject(AbpSession.GetTenantId(), fileBytes);
                await _binaryObjectManager.SaveAsync(logoObject);

                var tenant = await _tenantManager.GetByIdAsync(AbpSession.GetTenantId());

                tenant.LogoId       = logoObject.Id;
                tenant.LogoFileType = logoFile.ContentType;

                return(Json(new AjaxResponse(new { id = logoObject.Id, fileType = tenant.LogoFileType })));
            }
            catch (UserFriendlyException ex)
            {
                return(Json(new AjaxResponse(new ErrorInfo(ex.Message))));
            }
        }
Exemple #11
0
        public async Task <UploadProfilePictureOutputDto> UploadProfilePictureReturnFileId()
        {
            try
            {
                var profilePictureFile = Request.Form.Files.First();

                //Check input
                if (profilePictureFile == null)
                {
                    throw new UserFriendlyException(L("ProfilePicture_Change_Error"));
                }

                if (profilePictureFile.Length > MaxProfilePictureSize)
                {
                    throw new UserFriendlyException(L("ProfilePicture_Warn_SizeLimit", AppConsts.MaxProfilePictureBytesUserFriendlyValue));
                }

                byte[] fileBytes;
                using (var stream = profilePictureFile.OpenReadStream())
                {
                    fileBytes = stream.GetAllBytes();
                }

                if (!ImageFormatHelper.GetRawImageFormat(fileBytes).IsIn(ImageFormat.Jpeg, ImageFormat.Png, ImageFormat.Gif))
                {
                    throw new UserFriendlyException(L("ProfilePicture_Change_Info", AppConsts.MaxProfilePictureBytesUserFriendlyValue));
                }

                var storedFile = new DataFileObject(AbpSession.TenantId, fileBytes.ToArray());
                await _dataFileObjectManager.SaveAsync(storedFile);

                return(new UploadProfilePictureOutputDto
                {
                    FileName = profilePictureFile.FileName,
                    ProfilePictureId = storedFile.Id
                });
            }
            catch (UserFriendlyException ex)
            {
                return(new UploadProfilePictureOutputDto(new ErrorInfo(ex.Message)));
            }
        }
Exemple #12
0
        public async Task <JsonResult> UploadLogo()
        {
            try
            {
                if (Request.Files.Count <= 0 || Request.Files[0] == null)
                {
                    throw new UserFriendlyException(L("File_Empty_Error"));
                }

                var file = Request.Files[0];

                if (file.ContentLength > 30720) //30KB
                {
                    throw new UserFriendlyException(L("File_SizeLimit_Error"));
                }

                var fileBytes = file.InputStream.GetAllBytes();

                if (!ImageFormatHelper.GetRawImageFormat(fileBytes).IsIn(ImageFormat.Jpeg, ImageFormat.Png, ImageFormat.Gif))
                {
                    throw new UserFriendlyException("File_Invalid_Type_Error");
                }

                var logoObject = new BinaryObject(AbpSession.GetTenantId(), fileBytes);
                await _binaryObjectManager.SaveAsync(logoObject);

                var tenant = await _tenantManager.GetByIdAsync(AbpSession.GetTenantId());

                tenant.LogoId       = logoObject.Id;
                tenant.LogoFileType = file.ContentType;

                return(Json(new AjaxResponse(new { id = logoObject.Id, fileType = tenant.LogoFileType })));
            }
            catch (UserFriendlyException ex)
            {
                return(Json(new AjaxResponse(new ErrorInfo(ex.Message))));
            }
        }