예제 #1
0
        public async Task <AjaxResponse> Upload(UploadVModel model)
        {
            if (ModelState.IsValid)
            {
                string profilePhotoPath = string.Empty;
                string fileUrl          = UploadedFile(model, out profilePhotoPath);
                if (!string.IsNullOrEmpty(fileUrl))
                {
                    //Delete Old
                    if (model.IsDeleteOld)
                    {
                        var oldAttachmentList = (await _documentAppService.GetAllBusinessDocumentAttachments(null, model.BusinessDocumentId, model.BusinessEntityId)).Items;
                        foreach (var oldAttachment in oldAttachmentList)
                        {
                            await _documentAppService.DeleteBusinessDocumentAttachment(new BusinessDocumentAttachmentDto()
                            {
                                Id = oldAttachment.Id
                            });
                        }
                    }

                    var businessDocumentAttachmentDto = new BusinessDocumentAttachmentDto()
                    {
                        BusinessDocumentId = model.BusinessDocumentId,
                        BusinessEntityId   = model.BusinessEntityId,
                        FilePath           = fileUrl,
                        FileName           = System.IO.Path.GetFileNameWithoutExtension(model.Image.FileName),
                        FileExt            = System.IO.Path.GetExtension(model.Image.FileName),
                        Order = 0
                    };
                    var attachment = await _documentAppService.CreateBusinessDocumentAttachment(businessDocumentAttachmentDto);

                    if (attachment.Id > 0)
                    {
                        if (!string.IsNullOrEmpty(profilePhotoPath))//Profile Photo
                        {
                            ChangeProfilePhotoDto changeProfilePhotoDto = new ChangeProfilePhotoDto()
                            {
                                Id = model.BusinessEntityId, ProfilePhotoPath = businessDocumentAttachmentDto.Id.ToString()
                            };
                            await _userAppService.UpdateProfilePhoto(changeProfilePhotoDto);
                        }
                        return(new AjaxResponse()
                        {
                            Success = true, Result = attachment
                        });
                    }
                    else
                    {
                        return(new AjaxResponse(new ErrorInfo()
                        {
                            Message = "AttachmentNotAdded"
                        }));
                    }
                }
            }
            return(new AjaxResponse(new ErrorInfo()
            {
                Message = "Errors"
            }));
        }