Beispiel #1
0
        protected void getFileInfo(string sid, string path, string desPath, CompressModel msg, int quantity)
        {
            long size1 = getFileSize(path);
            long size2 = getFileSize(desPath);

            msg.rate       = getRate(size1, size2);
            msg.originSize = formatFileSize(size1);
            msg.size       = formatFileSize(size2);
            string rootPath = Request.ApplicationPath;

            msg.orginpath = (rootPath.EndsWith("/")? rootPath : rootPath + "/") + "Temps/" + sid + "/" + Path.GetFileName(path);
            msg.minpath   = (rootPath.EndsWith("/")? rootPath : rootPath + "/") + "Temps/" + sid + "/" + Path.GetFileName(desPath);
            msg.filename  = Path.GetFileName(path);
            msg.quantity  = quantity;

            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(fs);
                msg.orginwidth  = bitmap.Width;
                msg.orginheight = bitmap.Height;

                if (bitmap.RawFormat.Guid.Equals(ImageFormat.Png.Guid))
                {
                    msg.minQuantity = 1;
                    msg.maxQuantity = 256;
                }
                else if (bitmap.RawFormat.Guid.Equals(ImageFormat.Jpeg.Guid))
                {
                    msg.minQuantity = 10;
                    msg.maxQuantity = 100;
                }
                bitmap.Dispose();
            }
        }
Beispiel #2
0
        public JsonResult deleteFile()
        {
            string sid      = Request.QueryString["sid"];
            string filename = Request.QueryString["filename"];
            string dir      = Server.MapPath("~/temps");

            dir = dir + "\\" + sid;
            string path          = dir + "\\" + filename;
            string ext           = Path.GetExtension(path);
            string filenamenoext = Path.GetFileNameWithoutExtension(path);
            string desPath       = dir + "\\" + filenamenoext + "-min" + ext;

            if (System.IO.File.Exists(path))
            {
                System.IO.File.Delete(path);
            }
            if (System.IO.File.Exists(desPath))
            {
                System.IO.File.Delete(desPath);
            }

            JsonResult json = new JsonResult();

            json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            CompressModel msg = new CompressModel();

            msg.status = Constant.status_success;
            return(json);
        }
Beispiel #3
0
        public JsonResult getFileStatus()
        {
            String     fileDir = Server.MapPath("~/Temps/");
            string     sid     = Request.QueryString["sid"];
            string     sDir    = fileDir + "\\" + sid;
            JsonResult json    = new JsonResult();

            json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            if (!Directory.Exists(sDir))
            {
                CompressModel msg = new CompressModel();
                msg.status  = Constant.status_dir_notexist;
                msg.message = "文件夹不存在!";
                json.Data   = msg;
                return(json);
            }

            string filename = Request.QueryString["filename"];
            string path     = sDir + "\\" + filename;

            if (!System.IO.File.Exists(path))
            {
                CompressModel msg = new CompressModel();
                msg.status  = Constant.status_file_notexist;
                msg.message = Constant.message_file_notexist;
                msg.message = "文件不存在!";
                json.Data   = msg;
                return(json);
            }

            string ext            = Path.GetExtension(path);
            string filenamenotext = Path.GetFileNameWithoutExtension(path);
            string desPath        = sDir + "\\" + filenamenotext + "-min" + ext;

            if (!System.IO.File.Exists(desPath))
            {
                CompressModel msg = new CompressModel();
                msg.status  = Constant.status_file_notexist;
                msg.message = Constant.message_file_notexist;
                json.Data   = msg;
                return(json);
            }


            CompressModel result = new CompressModel();

            result.status  = Constant.status_compress_sucess;
            result.message = "";
            getFileInfo(sid, path, desPath, result, 90);
            json.Data = result;
            return(json);
        }
Beispiel #4
0
        private void transforpic(string path, string sid, CompressModel msg)
        {
            if (!System.IO.File.Exists(path))
            {
                return;
            }

            int    th_width  = 0;
            int    th_height = 0;
            string th_path;

            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(fs);
                int orginwidth  = bitmap.Width;
                int orginheight = bitmap.Height;

                th_width  = Constant.thumbnail_width;
                th_height = Constant.thumbnail_height;

                double rate1 = th_width * 1.0 / th_height;
                double rate2 = orginwidth * 1.0 / orginheight;
                if (rate1 >= rate2)
                {
                    th_width = orginwidth * th_height / orginheight;
                }
                else
                {
                    th_height = orginheight * th_width / orginwidth;
                }

                System.Drawing.Bitmap   th_bitmap = new System.Drawing.Bitmap(th_width, th_height);
                System.Drawing.Graphics g         = System.Drawing.Graphics.FromImage(th_bitmap);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.Default;
                g.Clear(System.Drawing.Color.White);
                //绘制缩略图
                g.DrawImage(bitmap, new System.Drawing.Rectangle(0, 0, th_width, th_height), new System.Drawing.Rectangle(0, 0, orginwidth, orginheight), System.Drawing.GraphicsUnit.Pixel);


                string th_dir           = Path.GetDirectoryName(path);
                string th_filenamenoext = Path.GetFileNameWithoutExtension(path);
                string ext = Path.GetExtension(path);
                th_path = th_dir + "\\" + th_filenamenoext + "-thumbnail" + ext;

                //保存缩略图
                th_bitmap.Save(th_path);

                g.Dispose();
                th_bitmap.Dispose();
                bitmap.Dispose();

                msg.thumbnailheight = th_height;
                msg.thumbnailwidth  = th_width;

                string rootPath = Request.ApplicationPath;
                rootPath = (rootPath.EndsWith("/") ? rootPath : rootPath + "/");

                msg.thumbnailpath = rootPath + "Temps/" + sid + "/" + Path.GetFileName(th_path);
            }
        }
Beispiel #5
0
        public JsonResult Upload()
        {
            JsonResult    json = new JsonResult();
            CompressModel msg  = new CompressModel();

            int count = Request.Files.Count;

            if (count < 1)
            {
                msg.status  = Constant.status_upload_fail;
                msg.message = "";
                json.Data   = msg;
                json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
                return(json);
            }

            String fileDir = Server.MapPath("~/Temps/");

            String sid = Request.QueryString["sid"];


            string sDir = fileDir + "\\" + sid;

            if (!Directory.Exists(sDir))
            {
                Directory.CreateDirectory(sDir);
            }
            string desPath = ""; String path = "";
            //for (int i = 0; i < count; i++)
            //{
            HttpPostedFileBase file = Request.Files.Get(0);

            //if (file == null) continue;
            String fileName = file.FileName;


            path = sDir + "\\" + fileName;

            file.SaveAs(path);

            int quantity = 0;

            desPath = compress(path, out quantity);

            //  break;
            //}


            transforpic(desPath, sid, msg);


            if (System.IO.File.Exists(desPath))
            {
                msg.status = Constant.status_compress_sucess;

                getFileInfo(sid, path, desPath, msg, quantity);
            }
            else
            {
                msg.status = Constant.status_compress_fail;
            }


            //msg.status = Constant.status_upload_success;
            //CompressModel status = new CompressModel();
            //status.status = Constant.status_upload_success;
            //msg.message="";
            json.Data = msg;
            json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return(json);
        }
Beispiel #6
0
        public JsonResult apply()
        {
            JsonResult json = new JsonResult();

            json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            string sid      = Request.QueryString["sid"];
            string filename = Request.QueryString["filename"];
            string dir      = Server.MapPath("~/temps/");

            dir += sid;
            string path          = dir + "\\" + filename;
            string filenamenoext = Path.GetFileNameWithoutExtension(path);
            string ext           = Path.GetExtension(path).ToLower();
            string desPath       = dir + "\\" + filenamenoext + "-min" + ext;

            if (!System.IO.File.Exists(path))
            {
                CompressModel model = new CompressModel();
                model.status  = Constant.status_file_notexist;
                model.message = Constant.message_file_notexist;
                json.Data     = model;
                return(json);
            }
            if (!System.IO.File.Exists(desPath))
            {
                CompressModel model = new CompressModel();
                model.status  = Constant.status_file_notexist;
                model.message = Constant.message_file_notexist;
                json.Data     = model;
                return(json);
            }
            string quanlitystr = Request.QueryString["quantity"];
            int    quanlity    = 0;

            if (!int.TryParse(quanlitystr, out quanlity))
            {
                CompressModel model = new CompressModel();
                model.status = Constant.status_parameter_error;
                json.Data    = model;
                return(json);
            }

            compress(path, quanlity);


            CompressModel result = new CompressModel();

            if (System.IO.File.Exists(desPath))
            {
                result.status = Constant.status_compress_sucess;
                getFileInfo(sid, path, desPath, result, quanlity);
            }
            else
            {
                result.status = Constant.status_compress_fail;
            }

            json.Data = result;
            json.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
            return(json);
        }