コード例 #1
0
        public PartialViewResult render(string mode, int modelId)
        {
            FileSelectorState fs = new FileSelectorState();
            dynamic           adminparam;
            string            propertyname, admintype = "";

            if (mode == "page")
            {
                PageModel pm = db.PageModel.Where(x => x.id == modelId).FirstOrDefault();
                adminparam   = pm.AdminParams();
                propertyname = pm.propertyname;
                admintype    = pm.admintype;
                fs.source    = "page";
            }
            else // resource
            {
                ResourceModel rm = db.ResourceModel.Where(x => x.id == modelId).FirstOrDefault();
                adminparam   = rm.AdminParams();
                propertyname = rm.propertyname;
                admintype    = rm.admintype;
                fs.source    = "resource";
            }

            fs.modelId         = modelId;
            fs.allowNewFolder  = adminparam.allownewfolder;
            fs.startFolder     = adminparam.folder;
            fs.currentFolder   = adminparam.folder;
            fs.imgh            = adminparam.imgh;
            fs.imgw            = adminparam.imgw;
            fs.cropmode        = adminparam.cropmode;
            fs.fillcolor       = adminparam.fillcolor;
            fs.parentControlId = "modelcontrol" + propertyname;
            fs.parentImgId     = admintype == "img" ? "modelcontrolimg" + propertyname : "";
            fs.uploadMode      = admintype == "img" ? "image"  : "file";
            fs.jsonData        = JsonConvert.SerializeObject(fs, new JsonSerializerSettings());


            return(PartialView("_FileSelector", fs));
        }
コード例 #2
0
        public PartialViewResult resizeImage(string mode, string fname, int modelId)
        {
            int     imgw, imgh = 0;
            string  cropmode = "";
            dynamic adminparam;


            if (mode == "page")
            {
                PageModel pm = db.PageModel.Where(x => x.id == modelId).FirstOrDefault();
                adminparam = pm.AdminParams();
            }
            else
            {
                ResourceModel rm = db.ResourceModel.Where(x => x.id == modelId).FirstOrDefault();
                adminparam = rm.AdminParams();
            }

            imgw     = adminparam.imgw;
            imgh     = adminparam.imgh;
            cropmode = adminparam.cropmode;

            return(render(mode, modelId));
        }
コード例 #3
0
        public JsonResult upload()
        {
            // lavoro con la request
            string folder = Request.Params["folder"];

            // read param fileprefix from http post request
            string fileprefix = Request.Params["fileprefix"];


            // FILE ABORTED
            if (Request.Files.Count == 0)
            {
                return(Json("[status:aborted]"));
            }

            // get 1st file in request
            HttpPostedFileBase f = Request.Files[0];
            // compone filename
            string fileName = fileprefix + f.FileName;


            string mode = Request.Params["mode"];

            if (!string.IsNullOrEmpty(mode))
            {
                // file o immagine caricata da una risorsa dinamica
                DbModel db      = new DbModel();
                int     modelId = Convert.ToInt32(Request.Params["modelId"]);
                string  admintype;
                dynamic adminparam;

                if (mode == "page")
                {
                    PageModel pm = db.PageModel.Where(x => x.id == modelId).FirstOrDefault();
                    admintype  = pm.admintype;
                    adminparam = pm.AdminParams();
                }
                else
                {
                    ResourceModel rm = db.ResourceModel.Where(x => x.id == modelId).FirstOrDefault();
                    admintype  = rm.admintype;
                    adminparam = rm.AdminParams();
                }


                string cropmode  = !string.IsNullOrEmpty(adminparam.cropmode) ? adminparam.cropmode : "none";
                string fillcolor = !string.IsNullOrEmpty(adminparam.fillcolor)? adminparam.fillcolor : "#ffffff";
                // cartella default
                string startfolder = adminparam.folder;
                startfolder = (startfolder.StartsWith("/") ? "" : "/") + startfolder;
                startfolder = startfolder + (startfolder.EndsWith("/") ? "" : "/");
                // cartella di destinazione
                string targetfolder = !string.IsNullOrEmpty(Request.Params["targetfolder"]) ? Request.Params["targetfolder"] : folder;
                targetfolder = (targetfolder.StartsWith("/") ? "" : "/") + targetfolder;
                targetfolder = targetfolder + (targetfolder.EndsWith("/") ? "" : "/");
                // cartella working
                string uploadfolder = (folder.StartsWith("/") ? "" : "/") + folder;
                uploadfolder = uploadfolder + (uploadfolder.EndsWith("/") ? "" : "/");

                // dimensioni di resize
                int imgh = adminparam.imgh != null ? adminparam.imgh : 0;
                int imgw = adminparam.imgw != null ? adminparam.imgw : 0;


                string originalfolder = startfolder + "original/";

                if (admintype == "img")
                {
                    // controllo che esista la cartella original, in caso contrario la creo
                    if (!System.IO.Directory.Exists(Server.MapPath(originalfolder)))
                    {
                        System.IO.Directory.CreateDirectory(Server.MapPath(originalfolder));
                    }


                    // salvo il file originale
                    f.SaveAs(Server.MapPath(originalfolder + fileName));
                    // attendo 2 secondi per il salvataggio sul disco
                    System.Threading.Thread.Sleep(2000);

                    // devo fare il crop dell'immagine
                    // in tal caso la cartella target è differente dalla cartella startFolder del modello
                    ImageResizer ir = new ImageResizer(Server.MapPath(originalfolder + fileName));
                    switch (cropmode)
                    {
                    case "fit":     //adatta e imposta il colore di fondo
                        ir.fit(imgw, imgh, fillcolor).Save(Server.MapPath(targetfolder + fileName));
                        break;

                    case "fill":     //riempi tagliando lo scarto
                        if (imgw == 0)
                        {
                            // altezza fissa
                            ir.resize(imgh, ImageResizer.fixedSize.Height).Save(Server.MapPath(targetfolder + fileName));
                        }
                        else if (imgh == 0)
                        {
                            // larghezza fissa
                            ir.resize(imgw, ImageResizer.fixedSize.Width).Save(Server.MapPath(targetfolder + fileName));
                        }
                        else
                        {
                            ir.crop(imgw, imgh).Save(Server.MapPath(targetfolder + fileName));
                        }
                        break;

                    default:
                        // copio il file nella cartella selezionata
                        f.SaveAs(Server.MapPath(uploadfolder + fileName));
                        break;
                    }
                }
                else
                {
                    f.SaveAs(Server.MapPath(uploadfolder + fileName));
                }
            }
            else
            {
                // save the file
                f.SaveAs(Server.MapPath(String.Format((folder.StartsWith("/") | folder.StartsWith("\\") ? "" : "../ ") + "{0}" + (folder.EndsWith("/") | folder.EndsWith("\\") ? "" : "/") + "{1}", folder, fileName)));
            }



            return(Json("[status:saved]"));
        }