示例#1
0
        public async Task <JsonResult> AddBanner(AddBanner banner, IFormCollection collection)
        {
            var files = collection.Files;

            if (files.Count > 0)
            {
                var    webRootPath     = _host.WebRootPath;
                string relativeDirPath = "\\BannerPic";
                string absolutePath    = webRootPath + relativeDirPath;

                string[] fileTypes = new string[] { ".gif", ".jpg", ".jpeg", ".png", ".bmp" };
                string   extension = Path.GetExtension(files[0].FileName);
                if (fileTypes.Contains(extension.ToLower()))
                {
                    if (!Directory.Exists(absolutePath))
                    {
                        Directory.CreateDirectory(absolutePath);
                    }
                    string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + extension;
                    var    filePath = absolutePath + "\\" + fileName;
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await files[0].CopyToAsync(stream);
                    }
                    banner.Image = "/BannerPic/" + fileName;
                    return(Json(_bannerService.AddBanner(banner)));
                }
                return(Json(new ResponseModel {
                    code = 0, result = "图片格式有误"
                }));
            }
            return(Json(new ResponseModel {
                code = 0, result = "请上传图片文件"
            }));
        }
示例#2
0
        public JsonResult Upload()
        {
            for (int i = 0; i < Request.Files.Count; i++)
            {
                HttpPostedFileBase file      = Request.Files[i]; //Uploaded file
                int              fileSize    = file.ContentLength;
                string           fileName    = file.FileName;
                string           mimeType    = file.ContentType;
                System.IO.Stream fileContent = file.InputStream;
                //To save file, use SaveAs method
                file.SaveAs(Server.MapPath("~/Images/Banners/") + fileName);
                int order = 0;

                var allBanners = BannerService.GetAllBanners();
                if (allBanners != null && allBanners.Any())
                {
                    order = allBanners.Count;
                }

                BannerService.AddBanner(new Banner
                {
                    FileName  = fileName,
                    ShowOrder = order + 1,
                    Status    = false
                });
            }
            return(Json("Uploaded " + Request.Files.Count + " files"));
        }
示例#3
0
        public async Task <JsonResult> AddBanner(AddBannerDto dto, IFormFile uploadFile)
        {
            if (uploadFile == null)
            {
                return(Json(new ResponseModel {
                    Code = 0, Result = "请上传图片!"
                }));
            }

            var webRootPath     = _hostEnvironment.ContentRootPath;
            var relativeDirPath = "UploadFiles\\BannerPic";
            var absolutePath    = Path.Combine(webRootPath, relativeDirPath);

            var fileTypes = new string[] { ".gif", ".jpg", ".jpeg", ".png", ".bmp" };
            var extension = Path.GetExtension(uploadFile.FileName);

            if (!fileTypes.Contains(extension.ToLower()))
            {
                return(Json(new ResponseModel {
                    Code = 0, Result = "图片格式有误!"
                }));
            }

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

            var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + extension;
            var filePath = Path.Combine(absolutePath, fileName);

            await using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await uploadFile.CopyToAsync(stream);
            }
            dto.Image = Path.Combine("/UploadFiles/BannerPic/", fileName);
            return(Json(_bannerService.AddBanner(dto)));
        }