コード例 #1
0
        public ActionResult AsyncUploadImage()
        {
            Stream stream      = null;
            var    fileName    = "";
            var    contentType = "";

            if (String.IsNullOrEmpty(Request["image"]))
            {
                // IE
                HttpPostedFileBase httpPostedFile = Request.Files[0];
                if (httpPostedFile == null)
                {
                    throw new ArgumentException("文件不存在");
                }
                stream      = httpPostedFile.InputStream;
                fileName    = Path.GetFileName(httpPostedFile.FileName);
                contentType = httpPostedFile.ContentType;
            }
            else
            {
                //Webkit, Mozilla-Request	{System.Web.HttpRequestWrapper}	System.Web.HttpRequestBase {System.Web.HttpRequestWrapper}

                stream   = Request.InputStream;
                fileName = Request["image"];
            }

            var fileBinary = new byte[stream.Length];

            stream.Read(fileBinary, 0, fileBinary.Length);

            var fileExtension = Path.GetExtension(fileName);

            if (!String.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }
            var isLocalStorage = _settingService.GetSettingByKey <bool>(MediaSettingNames.IsLocalStorage);
            var url            = string.Empty;

            if (isLocalStorage)
            {
                url = _imageService.UploadImage(images: fileBinary, isBuildThumbnail: fileBinary.Length > 30000);
            }
            else
            {
                url = _ossService.UploadImage(images: fileBinary, isBuildThumbnail: fileBinary.Length > 30000);
            }


            return(Json(new
            {
                success = true,
                Url = url,
            }));
        }
コード例 #2
0
        public ActionResult AsyncUploadImage()
        {
            Stream stream      = null;
            var    fileName    = "";
            var    contentType = "";

            if (String.IsNullOrEmpty(Request["image"]))
            {
                // IE
                HttpPostedFileBase httpPostedFile = Request.Files[0];
                if (httpPostedFile == null)
                {
                    throw new ArgumentException("文件不存在");
                }
                stream      = httpPostedFile.InputStream;
                fileName    = Path.GetFileName(httpPostedFile.FileName);
                contentType = httpPostedFile.ContentType;
            }
            else
            {
                //Webkit, Mozilla-		Request	{System.Web.HttpRequestWrapper}	System.Web.HttpRequestBase {System.Web.HttpRequestWrapper}

                stream   = Request.InputStream;
                fileName = Request["image"];
            }

            var productId = Convert.ToInt32(Request["productId"]);

            var fileBinary = new byte[stream.Length];

            stream.Read(fileBinary, 0, fileBinary.Length);

            var fileExtension = Path.GetExtension(fileName);

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

            var url = _imageService.UploadImage(images: fileBinary, isBuildThumbnail: true);

            _productImageService.InsertImage(new ProductImage
            {
                ProductId = productId,
                Url       = url
            });

            return(Json(new
            {
                success = true,
                Url = url,
            }));
        }
コード例 #3
0
        public ActionResult UploadAvatar()
        {
            if (Request.Files.Count <= 0)
            {
                return(AbpJson(null));
            }

            HttpPostedFileBase httpPostedFile = Request.Files[0];
            Stream             stream         = httpPostedFile.InputStream;
            var fileName    = Path.GetFileName(httpPostedFile.FileName);
            var contentType = httpPostedFile.ContentType;

            var fileBinary = new byte[stream.Length];

            stream.Read(fileBinary, 0, fileBinary.Length);

            var fileExtension = Path.GetExtension(fileName);

            if (!String.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }
            var mediaMode = _settingService.GetSettingByKey <MediaMode>(MediaSettingNames.MediaMode);
            var url       = string.Empty;

            if (mediaMode == MediaMode.Local)
            {
                url = "/images/default_avatar.png";
            }
            else if (mediaMode == MediaMode.Alyun)
            {
                url = _ossService.UploadImage(images: fileBinary, isBuildThumbnail: false);
            }


            var customer = GetCurrentCustomer();

            customer.SaveCustomerAttribute <string>(CustomerAttributeNames.Avatar, url);

            return(AbpJson(new
            {
                code = 0,
                msg = "",
                data = new
                {
                    src = url
                }
            }));
        }