Ejemplo n.º 1
0
        public async Task <PicData> InsertPicture([FromServices] IHostingEnvironment environment)
        {
            var    data       = new PicData();
            string path       = string.Empty;
            string foldername = "images";
            var    files      = Request.Form.Files;

            if (files == null || files.Count() <= 0)
            {
                data.Msg = "请选择上传的文件。"; return(data);
            }
            //格式限制
            var allowType = new string[] { "image/jpg", "image/png", "image/jpeg" };

            string folderpath = Path.Combine(environment.WebRootPath, foldername);

            if (!System.IO.Directory.Exists(folderpath))
            {
                System.IO.Directory.CreateDirectory(folderpath);
            }

            if (files.Any(c => allowType.Contains(c.ContentType)))
            {
                if (files.Sum(c => c.Length) <= 1024 * 1024 * 4)
                {
                    //foreach (var file in files)
                    var    file    = files.FirstOrDefault();
                    string strpath = Path.Combine(foldername, DateTime.Now.ToString("MMddHHmmss") + file.FileName);
                    path = Path.Combine(environment.WebRootPath, strpath);

                    using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                    {
                        await file.CopyToAsync(stream);
                    }

                    data = new PicData()
                    {
                        Url = strpath,
                        Msg = "上传成功",
                        Suc = true,
                    };
                    return(data);
                }
                else
                {
                    data.Msg = "图片过大";
                    return(data);
                }
            }
            else

            {
                data.Msg = "图片格式错误";
                return(data);
            }
        }
Ejemplo n.º 2
0
        public async Task <bool> InsertPicture([FromServices] IHostingEnvironment environment)
        {
            var    data  = new PicData();
            string path  = string.Empty;
            var    files = Request.Form.Files;

            if (files == null || files.Count() <= 0)
            {
                data.Msg = "请选择上传的文件。"; return(false);
            }
            //格式限制
            var allowType = new string[] { "image/jpg", "image/png", "image/jpeg" };

            if (files.Any(c => allowType.Contains(c.ContentType)))
            {
                if (files.Sum(c => c.Length) <= 1024 * 1024 * 4)
                {
                    foreach (var file in files)
                    {
                        string strpath = Path.Combine("Upload", DateTime.Now.ToString("MMddHHmmss") + file.FileName);
                        path = Path.Combine(environment.WebRootPath, strpath);

                        using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite))
                        {
                            await file.CopyToAsync(stream);
                        }
                    }
                    data.Msg = "上传成功";
                    return(true);
                }
                else
                {
                    data.Msg = "图片过大";
                    return(false);
                }
            }
            else

            {
                data.Msg = "图片格式错误";
                return(false);
            }
        }