コード例 #1
0
        public virtual IActionResult AsyncUpload()
        {
            var httpPostedFile = Request.Form.Files.FirstOrDefault();

            if (httpPostedFile == null)
            {
                return(Json(new
                {
                    success = false,
                    message = "No file uploaded",
                    downloadGuid = Guid.Empty
                }));
            }

            var fileBinary = _downloadService.GetDownloadBits(httpPostedFile);

            var qqFileNameParameter = "qqfilename";
            var fileName            = httpPostedFile.FileName;

            if (string.IsNullOrEmpty(fileName) && Request.Form.ContainsKey(qqFileNameParameter))
            {
                fileName = Request.Form[qqFileNameParameter].ToString();
            }
            //remove path (passed in IE)
            fileName = _fileProvider.GetFileName(fileName);

            var contentType = httpPostedFile.ContentType;

            var fileExtension = _fileProvider.GetFileExtension(fileName);

            if (!string.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }

            var download = new Download
            {
                DownloadGuid   = Guid.NewGuid(),
                UseDownloadUrl = false,
                DownloadUrl    = string.Empty,
                DownloadBinary = fileBinary,
                ContentType    = contentType,
                //we store filename without extension for downloads
                Filename  = _fileProvider.GetFileNameWithoutExtension(fileName),
                Extension = fileExtension,
                IsNew     = true
            };

            _downloadService.InsertDownload(download);

            //when returning JSON the mime-type must be set to text/plain
            //otherwise some browsers will pop-up a "Save As" dialog.
            return(Json(new
            {
                success = true,
                downloadId = download.Id,
                downloadUrl = Url.Action("DownloadFile", new { downloadGuid = download.DownloadGuid })
            }));
        }
コード例 #2
0
        public virtual ActionResult <Download> Post()
        {
            var httpPostedFile = Request.Form.Files.FirstOrDefault();

            if (httpPostedFile == null)
            {
                return(NotFound());
            }

            var fileBinary          = _downloadService.GetDownloadBits(httpPostedFile);
            var qqFileNameParameter = "qqfilename";
            var fileName            = httpPostedFile.FileName;

            if (string.IsNullOrEmpty(fileName) && Request.Form.ContainsKey(qqFileNameParameter))
            {
                fileName = Request.Form[qqFileNameParameter].ToString();
            }
            //remove path (passed in IE)
            fileName = _fileProvider.GetFileName(fileName);

            var contentType = httpPostedFile.ContentType;

            var fileExtension = _fileProvider.GetFileExtension(fileName);

            if (!string.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }

            var download = new Download
            {
                DownloadGuid   = Guid.NewGuid(),
                UseDownloadUrl = false,
                DownloadUrl    = string.Empty,
                DownloadBinary = fileBinary,
                ContentType    = contentType,
                //we store filename without extension for downloads
                Filename  = _fileProvider.GetFileNameWithoutExtension(fileName),
                Extension = fileExtension,
                IsNew     = true
            };

            try
            {
                _downloadService.InsertDownload(download);

                //when returning JSON the mime-type must be set to text/plain
                //otherwise some browsers will pop-up a "Save As" dialog.
                return(download);
            }
            catch (Exception exc)
            {
                _logger.Error(exc.Message, exc);

                return(BadRequest());
            }
        }
コード例 #3
0
        public virtual IActionResult ApplyVendorSubmit(ApplyVendorModel model, bool captchaValid, IFormFile uploadedFile)
        {
            if (!_vendorSettings.AllowCustomersToApplyForVendorAccount)
            {
                return(RedirectToRoute("HomePage"));
            }

            if (!_workContext.CurrentCustomer.IsRegistered())
            {
                return(Challenge());
            }

            //validate CAPTCHA
            if (_captchaSettings.Enabled && _captchaSettings.ShowOnApplyVendorPage && !captchaValid)
            {
                ModelState.AddModelError("", _captchaSettings.GetWrongCaptchaMessage(_localizationService));
            }

            var pictureId = 0;

            if (uploadedFile != null && !string.IsNullOrEmpty(uploadedFile.FileName))
            {
                try
                {
                    var contentType         = uploadedFile.ContentType;
                    var vendorPictureBinary = _downloadService.GetDownloadBits(uploadedFile);
                    var picture             = _pictureService.InsertPicture(vendorPictureBinary, contentType, null);

                    if (picture != null)
                    {
                        pictureId = picture.Id;
                    }
                }
                catch (Exception)
                {
                    ModelState.AddModelError("", _localizationService.GetResource("Vendors.ApplyAccount.Picture.ErrorMessage"));
                }
            }

            //vendor attributes
            var vendorAttributesXml = ParseVendorAttributes(model.Form);

            _vendorAttributeParser.GetAttributeWarnings(vendorAttributesXml).ToList()
            .ForEach(warning => ModelState.AddModelError(string.Empty, warning));

            if (ModelState.IsValid)
            {
                var description = Core.Html.HtmlHelper.FormatText(model.Description, false, false, true, false, false, false);
                //disabled by default
                var vendor = new Vendor
                {
                    Name  = model.Name,
                    Email = model.Email,
                    //some default settings
                    PageSize = 6,
                    AllowCustomersToSelectPageSize = true,
                    PageSizeOptions = _vendorSettings.DefaultVendorPageSizeOptions,
                    PictureId       = pictureId,
                    Description     = description
                };
                _vendorService.InsertVendor(vendor);
                //search engine name (the same as vendor name)
                var seName = _urlRecordService.ValidateSeName(vendor, vendor.Name, vendor.Name, true);
                _urlRecordService.SaveSlug(vendor, seName, 0);

                //associate to the current customer
                //but a store owner will have to manually add this customer role to "Vendors" role
                //if he wants to grant access to admin area
                _workContext.CurrentCustomer.VendorId = vendor.Id;
                _customerService.UpdateCustomer(_workContext.CurrentCustomer);

                //update picture seo file name
                UpdatePictureSeoNames(vendor);

                //save vendor attributes
                _genericAttributeService.SaveAttribute(vendor, GSVendorDefaults.VendorAttributes, vendorAttributesXml);

                //notify store owner here (email)
                _workflowMessageService.SendNewVendorAccountApplyStoreOwnerNotification(_workContext.CurrentCustomer,
                                                                                        vendor, _localizationSettings.DefaultAdminLanguageId);

                model.DisableFormInput = true;
                model.Result           = _localizationService.GetResource("Vendors.ApplyAccount.Submitted");
                return(View(model));
            }

            //If we got this far, something failed, redisplay form
            model = _vendorModelFactory.PrepareApplyVendorModel(model, false, true, vendorAttributesXml);
            return(View(model));
        }
コード例 #4
0
        public virtual IActionResult UploadFileReturnRequest()
        {
            if (!_orderSettings.ReturnRequestsEnabled || !_orderSettings.ReturnRequestsAllowFiles)
            {
                return(Json(new
                {
                    success = false,
                    downloadGuid = Guid.Empty,
                }));
            }

            var httpPostedFile = Request.Form.Files.FirstOrDefault();

            if (httpPostedFile == null)
            {
                return(Json(new
                {
                    success = false,
                    message = "No file uploaded",
                    downloadGuid = Guid.Empty,
                }));
            }

            var fileBinary = _downloadService.GetDownloadBits(httpPostedFile);

            var qqFileNameParameter = "qqfilename";
            var fileName            = httpPostedFile.FileName;

            if (string.IsNullOrEmpty(fileName) && Request.Form.ContainsKey(qqFileNameParameter))
            {
                fileName = Request.Form[qqFileNameParameter].ToString();
            }
            //remove path (passed in IE)
            fileName = _fileProvider.GetFileName(fileName);

            var contentType = httpPostedFile.ContentType;

            var fileExtension = _fileProvider.GetFileExtension(fileName);

            if (!string.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }

            var validationFileMaximumSize = _orderSettings.ReturnRequestsFileMaximumSize;

            if (validationFileMaximumSize > 0)
            {
                //compare in bytes
                var maxFileSizeBytes = validationFileMaximumSize * 1024;
                if (fileBinary.Length > maxFileSizeBytes)
                {
                    return(Json(new
                    {
                        success = false,
                        message = string.Format(_localizationService.GetResource("ShoppingCart.MaximumUploadedFileSize"), validationFileMaximumSize),
                        downloadGuid = Guid.Empty,
                    }));
                }
            }

            var download = new Download
            {
                DownloadGuid   = Guid.NewGuid(),
                UseDownloadUrl = false,
                DownloadUrl    = "",
                DownloadBinary = fileBinary,
                ContentType    = contentType,
                //we store filename without extension for downloads
                Filename  = _fileProvider.GetFileNameWithoutExtension(fileName),
                Extension = fileExtension,
                IsNew     = true
            };

            _downloadService.InsertDownload(download);

            //when returning JSON the mime-type must be set to text/plain
            //otherwise some browsers will pop-up a "Save As" dialog.
            return(Json(new
            {
                success = true,
                message = _localizationService.GetResource("ShoppingCart.FileUploaded"),
                downloadUrl = Url.Action("GetFileUpload", "Download", new { downloadId = download.DownloadGuid }),
                downloadGuid = download.DownloadGuid,
            }));
        }
コード例 #5
0
ファイル: PictureService.cs プロジェクト: kohanmh/srcNop
        /// <summary>
        /// Inserts a picture
        /// </summary>
        /// <param name="formFile">Form file</param>
        /// <param name="defaultFileName">File name which will be use if IFormFile.FileName not present</param>
        /// <param name="virtualPath">Virtual path</param>
        /// <returns>Picture</returns>
        public virtual Picture InsertPicture(IFormFile formFile, string defaultFileName = "", string virtualPath = "")
        {
            var imgExt = new List <string>
            {
                ".bmp",
                ".gif",
                ".jpeg",
                ".jpg",
                ".jpe",
                ".jfif",
                ".pjpeg",
                ".pjp",
                ".png",
                ".tiff",
                ".tif",
                ".ico"
            } as IReadOnlyCollection <string>;

            var fileName = formFile.FileName;

            if (string.IsNullOrEmpty(fileName) && !string.IsNullOrEmpty(defaultFileName))
            {
                fileName = defaultFileName;
            }

            //remove path (passed in IE)
            fileName = _fileProvider.GetFileName(fileName);

            var contentType = formFile.ContentType;

            var fileExtension = _fileProvider.GetFileExtension(fileName);

            if (!string.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }

            if (imgExt.All(ext => !ext.Equals(fileExtension, StringComparison.CurrentCultureIgnoreCase)))
            {
                return(null);
            }

            //contentType is not always available
            //that's why we manually update it here
            //http://www.sfsu.edu/training/mimetype.htm
            if (string.IsNullOrEmpty(contentType))
            {
                switch (fileExtension)
                {
                case ".bmp":
                    contentType = MimeTypes.ImageBmp;
                    break;

                case ".gif":
                    contentType = MimeTypes.ImageGif;
                    break;

                case ".jpeg":
                case ".jpg":
                case ".jpe":
                case ".jfif":
                case ".pjpeg":
                case ".pjp":
                    contentType = MimeTypes.ImageJpeg;
                    break;

                case ".png":
                    contentType = MimeTypes.ImagePng;
                    break;

                case ".tiff":
                case ".tif":
                    contentType = MimeTypes.ImageTiff;
                    break;

                default:
                    break;
                }
            }

            var picture = InsertPicture(_downloadService.GetDownloadBits(formFile), contentType, _fileProvider.GetFileNameWithoutExtension(fileName));

            if (string.IsNullOrEmpty(virtualPath))
            {
                return(picture);
            }

            picture.VirtualPath = _fileProvider.GetVirtualPath(virtualPath);
            _pictureRepository.Update(picture);

            return(picture);
        }
コード例 #6
0
ファイル: PictureController.cs プロジェクト: agsyazilim/Ags
        //do not validate request token (XSRF)


        public virtual IActionResult AsyncUpload()
        {
            //if (!_permissionService.Authorize(StandardPermissionProvider.UploadPictures))
            //    return Json(new { success = false, error = "You do not have required permissions" }, "text/plain");


            IFormFile httpPostedFile = Request.Form.Files.FirstOrDefault();

            if (httpPostedFile == null)
            {
                return(Json(new
                {
                    success = false,
                    message = "No file uploaded",
                    downloadGuid = Guid.Empty
                }));
            }

            byte[] fileBinary = _downloadService.GetDownloadBits(httpPostedFile);

            const string qqFileNameParameter = "qqfilename";
            string       fileName            = httpPostedFile.FileName;

            if (string.IsNullOrEmpty(fileName) && Request.Form.ContainsKey(qqFileNameParameter))
            {
                fileName = Request.Form[qqFileNameParameter].ToString();
            }
            //remove path (passed in IE)
            fileName = _fileProvider.GetFileName(fileName);

            string contentType = httpPostedFile.ContentType;

            string fileExtension = _fileProvider.GetFileExtension(fileName);

            if (!string.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }

            //contentType is not always available
            //that's why we manually update it here
            //http://www.sfsu.edu/training/mimetype.htm
            if (string.IsNullOrEmpty(contentType))
            {
                switch (fileExtension)
                {
                case ".bmp":
                    contentType = MimeTypes.ImageBmp;
                    break;

                case ".gif":
                    contentType = MimeTypes.ImageGif;
                    break;

                case ".jpeg":
                case ".jpg":
                case ".jpe":
                case ".jfif":
                case ".pjpeg":
                case ".pjp":
                    contentType = MimeTypes.ImageJpeg;
                    break;

                case ".png":
                    contentType = MimeTypes.ImagePng;
                    break;

                case ".tiff":
                case ".tif":
                    contentType = MimeTypes.ImageTiff;
                    break;

                default:
                    break;
                }
            }

            var picture = _pictureService.InsertPicture(fileBinary, contentType, null);

            //when returning JSON the mime-type must be set to text/plain
            //otherwise some browsers will pop-up a "Save As" dialog.
            return(Json(new { success = true, pictureId = picture.Id, imageUrl = _pictureService.GetPictureUrl(picture.Id, 100) }));
        }
コード例 #7
0
        public virtual object UploadImage(IFormFile file)
        {
            if (file == null)
            {
                throw new AbpException("No file uploaded");
            }

            var fileBinary = downloadService.GetDownloadBits(file);

            const string qqFileNameParameter = "qqfilename";
            var          fileName            = file.FileName;

            if (string.IsNullOrEmpty(fileName) && Request.Form.ContainsKey(qqFileNameParameter))
            {
                fileName = Request.Form[qqFileNameParameter].ToString();
            }
            //remove path (passed in IE)
            fileName = fileProvider.GetFileName(fileName);

            var contentType = file.ContentType;

            var fileExtension = fileProvider.GetFileExtension(fileName);

            if (!string.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }

            if (string.IsNullOrEmpty(contentType))
            {
                switch (fileExtension)
                {
                case ".bmp":
                    contentType = MimeTypes.ImageBmp;
                    break;

                case ".gif":
                    contentType = MimeTypes.ImageGif;
                    break;

                case ".jpeg":
                case ".jpg":
                case ".jpe":
                case ".jfif":
                case ".pjpeg":
                case ".pjp":
                    contentType = MimeTypes.ImageJpeg;
                    break;

                case ".png":
                    contentType = MimeTypes.ImagePng;
                    break;

                case ".tiff":
                case ".tif":
                    contentType = MimeTypes.ImageTiff;
                    break;

                default:
                    break;
                }
            }

            var picture = pictureAppService.InsertPicture(fileBinary, contentType, null);

            return(new { Id = picture.Id, Path = pictureAppService.GetPictureUrl(picture.Id, 200) });
        }
コード例 #8
0
        public virtual IActionResult UploadAvatar(UserAvatarModel model, IFormFile uploadedFile)
        {
            if (!_workContext.CurrentUser.IsRegistered())
            {
                return(Challenge());
            }

            if (!_userSettings.AllowUsersToUploadAvatars)
            {
                return(RedirectToRoute("UserInfo"));
            }

            var user = _workContext.CurrentUser;

            if (ModelState.IsValid)
            {
                try
                {
                    var userAvatar = _pictureService.GetPictureById(_genericAttributeService.GetAttribute <int>(user, NopUserDefaults.AvatarPictureIdAttribute));
                    if (uploadedFile != null && !string.IsNullOrEmpty(uploadedFile.FileName))
                    {
                        var avatarMaxSize = _userSettings.AvatarMaximumSizeBytes;
                        if (uploadedFile.Length > avatarMaxSize)
                        {
                            throw new NopException(string.Format(_localizationService.GetResource("Account.Avatar.MaximumUploadedFileSize"), avatarMaxSize));
                        }

                        var userPictureBinary = _downloadService.GetDownloadBits(uploadedFile);
                        if (userAvatar != null)
                        {
                            userAvatar = _pictureService.UpdatePicture(userAvatar.Id, userPictureBinary, uploadedFile.ContentType, null);
                        }
                        else
                        {
                            userAvatar = _pictureService.InsertPicture(userPictureBinary, uploadedFile.ContentType, null);
                        }
                    }

                    var userAvatarId = 0;
                    if (userAvatar != null)
                    {
                        userAvatarId = userAvatar.Id;
                    }

                    _genericAttributeService.SaveAttribute(user, NopUserDefaults.AvatarPictureIdAttribute, userAvatarId);

                    model.AvatarUrl = _pictureService.GetPictureUrl(
                        _genericAttributeService.GetAttribute <int>(user, NopUserDefaults.AvatarPictureIdAttribute),
                        _mediaSettings.AvatarPictureSize,
                        false);
                    return(View(model));
                }
                catch (Exception exc)
                {
                    ModelState.AddModelError("", exc.Message);
                }
            }

            //If we got this far, something failed, redisplay form
            model = _userModelFactory.PrepareUserAvatarModel(model);
            return(View(model));
        }
コード例 #9
0
        public IActionResult UploadFileProductAttribute(int attributeId)
        {
            var attribute = _productAttributeService.GetProductAttributeMappingById(attributeId);

            if (attribute == null || attribute.AttributeControlType != AttributeControlType.FileUpload)
            {
                return(Json(new
                {
                    success = false,
                    downloadGuid = Guid.Empty
                }));
            }

            var httpPostedFile = Request.Form.Files.FirstOrDefault();

            if (httpPostedFile == null)
            {
                return(Json(new
                {
                    success = false,
                    message = "No file uploaded",
                    downloadGuid = Guid.Empty
                }));
            }

            var fileBinary = _downloadService.GetDownloadBits(httpPostedFile);

            var qqFileNameParameter = "qqfilename";
            var fileName            = httpPostedFile.FileName;

            if (string.IsNullOrEmpty(fileName) && Request.Form.ContainsKey(qqFileNameParameter))
            {
                fileName = Request.Form[qqFileNameParameter].ToString();
            }
            //remove path (passed in IE)
            fileName = _fileProvider.GetFileName(fileName);

            var contentType = httpPostedFile.ContentType;

            var fileExtension = _fileProvider.GetFileExtension(fileName);

            if (!string.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }

            if (attribute.ValidationFileMaximumSize.HasValue)
            {
                //compare in bytes
                var maxFileSizeBytes = attribute.ValidationFileMaximumSize.Value * 1024;
                if (fileBinary.Length > maxFileSizeBytes)
                {
                    //when returning JSON the mime-type must be set to text/plain
                    //otherwise some browsers will pop-up a "Save As" dialog.
                    return(Json(new
                    {
                        success = false,
                        message = string.Format(_localizationService.GetResource("ShoppingCart.MaximumUploadedFileSize"), attribute.ValidationFileMaximumSize.Value),
                        downloadGuid = Guid.Empty
                    }));
                }
            }

            var download = new Download
            {
                DownloadGuid   = Guid.NewGuid(),
                UseDownloadUrl = false,
                DownloadUrl    = "",
                DownloadBinary = fileBinary,
                ContentType    = contentType,
                //we store filename without extension for downloads
                Filename  = _fileProvider.GetFileNameWithoutExtension(fileName),
                Extension = fileExtension,
                IsNew     = true
            };

            _downloadService.InsertDownload(download);

            //generate new image merging the uploaded custom logo and product picture
            //newly added features for the override
            var logoPosition = _logoPositionService.GetByProductId(attribute.ProductId);

            (int, string)imagePath = _customLogoService.MergeProductPictureWithLogo(
                download.DownloadBinary,
                download.Id,
                attribute.ProductId,
                ProductPictureModifierUploadType.Custom,
                logoPosition.Size,
                logoPosition.Opacity,
                logoPosition.XCoordinate,
                logoPosition.YCoordinate,
                _mediaSettings.ProductDetailsPictureSize
                );
            //when returning JSON the mime-type must be set to text/plain
            //otherwise some browsers will pop-up a "Save As" dialog.
            return(Json(new
            {
                success = true,
                message = _localizationService.GetResource("ShoppingCart.FileUploaded"),
                downloadUrl = Url.Action("GetFileUpload", "Download", new { downloadId = download.DownloadGuid }),
                downloadGuid = download.DownloadGuid,
                imagePath = imagePath.Item2, //added property for the override
            }));
        }