Ejemplo n.º 1
0
        public JsonResult UploadFileByEditor(string savepath)
        {
            var rf = new AjaxModel();

            var obj = new ReturnObj()
            {
                error = 1, url = ""
            };

            HttpPostedFileBase file = Request.Files["imgFile"];

            var currentPath = "/Files/TempFiles/";

            var filePath = System.AppDomain.CurrentDomain.BaseDirectory + currentPath + savepath;

            if (!DirectoryHelper.IsExistDirectory(filePath))
            {
                DirectoryHelper.CreateDirectory(filePath);
            }

            string saveUrl = currentPath + savepath + "/";

            var imageList = new List <string>()
            {
                ".gif", ".jpg", ".jpeg", ".png", ".bmp"
            };
            string fileName      = file.FileName;
            string fileExt       = System.IO.Path.GetExtension(fileName).ToLower();
            var    accessoryName = fileName.Split('.')[0];

            if (imageList.Contains(fileExt))
            {
                rf = SaveUploadFile(file, saveUrl, "image");
            }
            else
            {
                rf = SaveUploadFile(file, saveUrl, "file");
            }

            if (rf.Value != null)
            {
                obj.error = 0;
                obj.url   = "/Web" + rf.Value.ToString();
            }

            return(Json(obj, JsonRequestBehavior.AllowGet));
        }
Ejemplo n.º 2
0
        //消息模块富文本编辑器
        public JsonResult UploadThumbnail3(string savepath)
        {
            var obj = new ReturnObj()
            {
                error = 1, url = ""
            };

            HttpPostedFileBase file = Request.Files["imgFile"];
            Image resourceImage     = Image.FromStream(file.InputStream);

            var currentPath = "/Files/EntMgr/EntMsg/";

            // 文件的绝对路径地址
            var filePath = System.AppDomain.CurrentDomain.BaseDirectory + currentPath + savepath;

            if (!DirectoryHelper.IsExistDirectory(filePath))
            {
                DirectoryHelper.CreateDirectory(filePath);
            }

            string saveUrl = currentPath + savepath + "/";

            var imageList = new List <string>()
            {
                ".gif", ".jpg", ".jpeg", ".png", ".bmp"
            };
            string fileName      = file.FileName;
            string fileExt       = System.IO.Path.GetExtension(fileName).ToLower();
            var    accessoryName = fileName.Split('.')[0];

            if (imageList.Contains(fileExt))
            {
                var compressWidth  = 0;
                var compressHeight = 0;

                var primaryWidth  = resourceImage.Width;
                var primaryHeight = resourceImage.Height;

                if (primaryWidth <= 1000 && primaryHeight <= 800)
                {
                    compressWidth  = primaryWidth;
                    compressHeight = primaryHeight;
                }
                else
                {
                    compressWidth  = 1000;
                    compressHeight = 800;
                }

                var saveFileName = DateTime.Now.ToString("yyyyMMddHHmmss_ffff") + fileExt;

                //用指定的大小和格式初始化Bitmap类的新实例
                Bitmap bitmap = new Bitmap(compressWidth, compressHeight, PixelFormat.Format32bppArgb);
                //从指定的Image对象创建新Graphics对象
                Graphics graphics = Graphics.FromImage(bitmap);
                //清除整个绘图面并以透明背景色填充
                graphics.Clear(Color.Transparent);
                //在指定位置并且按指定大小绘制原图片对象
                graphics.DrawImage(resourceImage, new Rectangle(0, 0, compressWidth, primaryHeight));

                bitmap.Save(System.AppDomain.CurrentDomain.BaseDirectory + saveUrl + saveFileName, ImageFormat.Jpeg);

                obj.error = 0;
                string   url   = AppDomain.CurrentDomain.BaseDirectory;
                string[] array = url.Split('/');
                int      lenth = array.Length;
                obj.url = "/" + array[lenth - 1] + saveUrl + saveFileName;
                //obj.url = System.Configuration.ConfigurationManager.AppSettings["EditAddress"] + saveUrl + saveFileName;
            }

            return(Json(obj, JsonRequestBehavior.AllowGet));
        }