Exemplo n.º 1
0
        public IActionResult GetUploadSet()
        {
            var result = new ResultAdaptDto();
            var set    = SiteManagerCache.GetUploadInfo();

            result.data.Add("uploadSet", set);
            return(Content(result.ToJson()));
        }
Exemplo n.º 2
0
        public ActionResult Index(string action)
        {
            var result = new UeditorUploadDto();

            try
            {
                if (action == "config")
                {
                    string configPath = $@"ui\plugins\ueditor\config.json";
                    string serverPath = Path.Combine(GlobalParamsDto.WebRoot, configPath);
                    if (System.IO.File.Exists(serverPath))
                    {
                        string json = System.IO.File.ReadAllText(serverPath);
                        return(Content(json));
                    }
                    return(Content(""));
                }

                var files = Request.Form.Files;
                if (files.Count == 0)
                {
                    result.state = "FAIL";
                    result.error = "请选择文件";
                    return(Content(result.ToJson()));
                }
                var    file     = files[0];
                var    filename = file.FileName;
                int    index    = filename.LastIndexOf('.');
                string extName  = filename.Substring(index + 1);
                //uploadimage
                string url       = $@"upfiles\images\{DateTime.Now.ToString("yyyyMMdd")}";
                var    uploadSet = SiteManagerCache.GetUploadInfo();
                if (action == "uploadimage")
                {
                    var imageExt = uploadSet.image_extname.Split(',');
                    if (!imageExt.Contains(extName))
                    {
                        result.state = "FAIL";
                        result.error = $"禁止上传图片类型:{extName}";
                        return(Content(result.ToJson()));
                    }
                }
                else if (action == "uploadfile")
                {
                    var fileExt = uploadSet.attache_extname.Split(',');
                    if (!fileExt.Contains(extName))
                    {
                        result.state = "FAIL";
                        result.error = $"禁止上传附件类型:{extName}";
                        return(Content(result.ToJson()));
                    }

                    if (file.Length > (uploadSet.max_file_size * 1024 * 1024))
                    {
                        result.state = "FAIL";
                        result.error = $"上传附件超过{uploadSet.max_file_size}MB限制,禁止上传";
                        return(Content(result.ToJson()));
                    }
                    url = $@"upfiles\attachments\{DateTime.Now.ToString("yyyyMMdd")}";
                }
                else if (action == "uploadvideo")
                {
                    var mediaExt = uploadSet.media_extname.Split(',');
                    if (!mediaExt.Contains(extName))
                    {
                        result.state = "FAIL";
                        result.error = $"禁止上传视频类型:{extName}";
                        return(Content(result.ToJson()));
                    }
                    if (file.Length > (uploadSet.max_file_size * 1024 * 1024))
                    {
                        result.state = "FAIL";
                        result.error = $"上传视频超过{uploadSet.max_file_size}MB限制,禁止上传";
                        return(Content(result.ToJson()));
                    }
                    url = $@"upfiles\videos\{DateTime.Now.ToString("yyyyMMdd")}";
                }
                var folder = Path.Combine(GlobalParamsDto.WebRoot, url);

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

                string guidFileName = $"{IdHelper.ObjectId()}.{extName}";
                //这个hostingEnv.WebRootPath就是要存的地址可以改下
                string newfilename = Path.Combine(folder, guidFileName);

                using (FileStream fs = System.IO.File.Create(newfilename))
                {
                    file.CopyTo(fs);
                    fs.Flush();
                }
                if (action == "uploadimage")
                {
                    var firstFileInfo = new FileInfo(newfilename);
                    if (firstFileInfo.Length > 200 * 1024)
                    {
                        string compressFileName = $"{IdHelper.ObjectId()}.{extName}";
                        string compressFile     = $@"{folder}\{compressFileName}";
                        ImageUtilities.CompressImage(newfilename, compressFile, 90, 200);
                        guidFileName = compressFileName;
                    }
                }
                result.original = filename;
                result.title    = filename;
                result.url      = $@"\{url}\{guidFileName}";
            }
            catch (Exception e)
            {
                LoggerHelper.Exception(e);
                result.state = "FAIL";
                result.error = "内部异常,请联系管理员";
            }


            return(Content(result.ToJson()));
        }
Exemplo n.º 3
0
        public ActionResult Uploadimg()
        {
            var result = new ResultAdaptDto();
            //long size = 0;
            //当设置了开始水印的时候,可以使用nomark来过滤图片不加水印
            int nomark = RequestHelper.GetPostInt("nomark");
            var files  = Request.Form.Files;

            if (files.Count == 0)
            {
                result.status  = false;
                result.message = "没有文件信息";
                return(Content(result.ToJson()));
            }
            string url    = $"/upfiles/images/{DateTime.Now.ToString("yyyyMMdd")}";
            var    folder = GlobalContext.WebRootPath + url;

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

            var file     = files[0];
            var filename = ContentDispositionHeaderValue
                           .Parse(file.ContentDisposition)
                           .FileName
                           .Trim('"');
            int    index        = filename.LastIndexOf('.');
            string extName      = filename.Substring(index);
            string guidstr      = Guid.NewGuid().ToString("N");
            string guidFileName = guidstr + extName;

            //这个hostingEnv.WebRootPath就是要存的地址可以改下
            filename = $"{folder}/{guidFileName}";
            using (FileStream fs = System.IO.File.Create(filename))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            var firstFileInfo = new FileInfo(filename);

            if (firstFileInfo.Length > 200 * 1024)
            {
                string compressFileName = IdHelper.ObjectId() + extName;
                string compressFile     = $"{folder}/{compressFileName}";
                ImageUtilities.CompressImage(filename, compressFile, 90, 200);
                guidFileName = compressFileName;
            }
            if (nomark == 0)
            {
                var imageSet = SiteManagerCache.GetUploadInfo();
                if (imageSet.open_watermark == 1)
                {
                    try
                    {
                        string sourcePath = $"{folder}/{guidFileName}";
                        if (System.IO.File.Exists(sourcePath))
                        {
                            FileStream fs = new FileStream(sourcePath, FileMode.Open);
                            //把文件读取到字节数组
                            byte[] data = new byte[fs.Length];
                            fs.Read(data, 0, data.Length);
                            fs.Close();
                            //实例化一个内存流--->把从文件流中读取的内容[字节数组]放到内存流中去
                            MemoryStream ms    = new MemoryStream(data);
                            Image        image = Image.FromStream(ms);
                            if (image.Width > imageSet.image_width && image.Height > imageSet.image_height)
                            {
                                ImageWatermarker marker = new ImageWatermarker();
                                //图片水印
                                if (imageSet.watermark_type == 1)
                                {
                                    string waterMarkIamge = GlobalContext.WebRootPath + imageSet.watermark_image;
                                    if (System.IO.File.Exists(waterMarkIamge))
                                    {
                                        marker.AddImageSignPic(image, sourcePath, waterMarkIamge, imageSet.water_postion, imageSet.image_quality, imageSet.image_opacity);
                                    }
                                }
                                else
                                {
                                    marker.AddWatermarkText(image, sourcePath, imageSet.watermark_word, imageSet.water_postion, imageSet.font_size, imageSet.font_color);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        LoggerHelper.Exception(ex);
                    }
                }
            }
            string imgurl = $"{ url}/{guidFileName}";

            result.data.Add("url", imgurl);
            return(Content(result.ToJson()));
        }