public FileUploadResult ProcessUpload(HttpContext context)
        {
            var fileUploadResult = new FileUploadResult();

            if (!ProgressFileUploader.HasFilesToUpload(context))
            {
                return(fileUploadResult);
            }

            var file = new ProgressFileUploader.FileToUpload(context);

            if (String.IsNullOrEmpty(file.FileName) || file.ContentLength == 0)
            {
                throw new InvalidOperationException("Invalid file.");
            }

            if (0 < SetupInfo.MaxImageUploadSize && SetupInfo.MaxImageUploadSize < file.ContentLength)
            {
                throw FileSizeComment.FileImageSizeException;
            }

            if (FileUtility.GetFileTypeByFileName(file.FileName) != FileType.Image)
            {
                throw new UnknownImageFormatException();
            }

            var photoUri = ContactPhotoManager.UploadPhoto(file.InputStream, Convert.ToInt32(context.Request["contactID"]));

            fileUploadResult.Success = true;
            fileUploadResult.Data    = photoUri;

            return(fileUploadResult);
        }
示例#2
0
        public FileUploadResult ProcessUpload(HttpContext context)
        {
            var fileUploadResult = new FileUploadResult();

            if (!ProgressFileUploader.HasFilesToUpload(context))
            {
                return(fileUploadResult);
            }

            var file = new ProgressFileUploader.FileToUpload(context);

            if (String.IsNullOrEmpty(file.FileName) || file.ContentLength == 0)
            {
                throw new InvalidOperationException("Invalid file.");
            }

            if (0 < SetupInfo.MaxImageUploadSize && SetupInfo.MaxImageUploadSize < file.ContentLength)
            {
                fileUploadResult.Success = false;
                fileUploadResult.Message = FileSizeComment.GetFileImageSizeNote(CRMCommonResource.ErrorMessage_UploadFileSize, false).HtmlEncode();
                return(fileUploadResult);
            }

            if (FileUtility.GetFileTypeByFileName(file.FileName) != FileType.Image)
            {
                fileUploadResult.Success = false;
                fileUploadResult.Message = CRMJSResource.ErrorMessage_NotImageSupportFormat.HtmlEncode();
                return(fileUploadResult);
            }


            var contactId  = Convert.ToInt32(context.Request["contactID"]);
            var uploadOnly = Convert.ToBoolean(context.Request["uploadOnly"]);
            var tmpDirName = Convert.ToString(context.Request["tmpDirName"]);
            var photoUri   = "";

            if (contactId != 0)
            {
                photoUri = ContactPhotoManager.UploadPhoto(file.InputStream, contactId, uploadOnly);
            }
            else
            {
                if (String.IsNullOrEmpty(tmpDirName))
                {
                    tmpDirName = Guid.NewGuid().ToString();
                }
                photoUri = ContactPhotoManager.UploadPhoto(file.InputStream, tmpDirName);
            }
            fileUploadResult.Success = true;
            fileUploadResult.Data    = photoUri;

            return(fileUploadResult);
        }
示例#3
0
        public FileUploadResult ProcessUpload(HttpContext context)
        {
            if (!WebItemSecurity.IsAvailableForUser(ProductEntryPoint.ID.ToString(), SecurityContext.CurrentAccount.ID))
            {
                throw CRMSecurity.CreateSecurityException();
            }

            var     contactId = Convert.ToInt32(context.Request["contactID"]);
            Contact contact   = null;

            if (contactId != 0)
            {
                contact = Global.DaoFactory.GetContactDao().GetByID(contactId);
                if (!CRMSecurity.CanAccessTo(contact))
                {
                    throw CRMSecurity.CreateSecurityException();
                }
            }

            var fileUploadResult = new FileUploadResult();

            if (!FileToUpload.HasFilesToUpload(context))
            {
                return(fileUploadResult);
            }

            var file = new FileToUpload(context);

            if (String.IsNullOrEmpty(file.FileName) || file.ContentLength == 0)
            {
                throw new InvalidOperationException(CRMErrorsResource.InvalidFile);
            }

            if (0 < SetupInfo.MaxImageUploadSize && SetupInfo.MaxImageUploadSize < file.ContentLength)
            {
                fileUploadResult.Success = false;
                fileUploadResult.Message = FileSizeComment.GetFileImageSizeNote(CRMCommonResource.ErrorMessage_UploadFileSize, false).HtmlEncode();
                return(fileUploadResult);
            }

            if (FileUtility.GetFileTypeByFileName(file.FileName) != FileType.Image)
            {
                fileUploadResult.Success = false;
                fileUploadResult.Message = CRMJSResource.ErrorMessage_NotImageSupportFormat.HtmlEncode();
                return(fileUploadResult);
            }

            var uploadOnly = Convert.ToBoolean(context.Request["uploadOnly"]);
            var tmpDirName = Convert.ToString(context.Request["tmpDirName"]);

            try
            {
                string photoUri;
                if (contactId != 0)
                {
                    photoUri = ContactPhotoManager.UploadPhoto(file.InputStream, contactId, uploadOnly);
                }
                else
                {
                    if (String.IsNullOrEmpty(tmpDirName))
                    {
                        tmpDirName = Guid.NewGuid().ToString();
                    }
                    photoUri = ContactPhotoManager.UploadPhoto(file.InputStream, tmpDirName);
                }

                fileUploadResult.Success = true;
                fileUploadResult.Data    = photoUri;
            }
            catch (Exception e)
            {
                fileUploadResult.Success = false;
                fileUploadResult.Message = e.Message.HtmlEncode();
                return(fileUploadResult);
            }

            if (contact != null)
            {
                var messageAction = contact is Company ? MessageAction.CompanyUpdatedPhoto : MessageAction.PersonUpdatedPhoto;
                MessageService.Send(context.Request, messageAction, MessageTarget.Create(contact.ID), contact.GetTitle());
            }

            return(fileUploadResult);
        }