Exemplo n.º 1
0
    protected void Page_Load(object sender, EventArgs e)
    {
        bool   isValidFile     = false;
        string validExtension  = string.Empty;
        string retFilePath     = string.Empty;
        string retMsg          = "fail";
        int    maxFileSize     = 0;
        int    retStatus       = -1;
        string strBaseLocation = string.Empty;
        string filename        = string.Empty;
        Random rnd             = new Random();

        if (Request.Form["IsCropping"] == null)
        {
            if (Request.Form["ValidExtension"] != null)
            {
                validExtension = Request.Form["ValidExtension"].ToString();
            }
            if (Request.Form["BaseLocation"] != null)
            {
                strBaseLocation = Request.Form["BaseLocation"].ToString();
            }
            if (Request.Form["MaxFileSize"] != null && Request.Form["MaxFileSize"] != "" &&
                int.Parse(Request.Form["MaxFileSize"].ToString()) > 0)
            {
                maxFileSize = int.Parse(Request.Form["MaxFileSize"].ToString());
            }
            if (Request.Files != null)
            {
                HttpFileCollection files = Request.Files;
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile file = files[i];
                    if (file.ContentLength > 0 && file.ContentLength < maxFileSize * 1024)
                    {
                        if (validExtension.Length > 0)
                        {
                            string[] allowExtensions = validExtension.Split(' ');
                            if (allowExtensions.Contains(GetExtension(file.FileName),
                                                         StringComparer.InvariantCultureIgnoreCase))
                            {
                                isValidFile = true;
                                retMsg      = "Valid file Extension";
                            }
                            else
                            {
                                retMsg = "Not valid file Extension";
                            }
                        }
                        else
                        {
                            isValidFile = true;
                        }
                        if (isValidFile)
                        {
                            retFilePath     = strBaseLocation;
                            strBaseLocation = Server.MapPath("~/" + strBaseLocation);
                            if (!Directory.Exists(strBaseLocation))
                            {
                                Directory.CreateDirectory(strBaseLocation);
                            }
                            filename = System.IO.Path.GetFileName(file.FileName);
                            string strExtension = GetExtension(filename);
                            filename = filename.Substring(0, (filename.Length - strExtension.Length) - 1);
                            filename = filename + '_' + rnd.Next(111111, 999999).ToString() + '.' + strExtension;
                            string filePath = strBaseLocation + "\\" + filename;
                            retFilePath = retFilePath + "/" + filename;
                            file.SaveAs(filePath);
                            retMsg    = "File upload successfully";
                            retStatus = 1;
                        }
                    }
                    else
                    {
                        retMsg = "Sorry, the file must be less than " + maxFileSize + "KB";
                    }
                }
            }
            Literal lit = new Literal();
            lit.Text = "<pre id='response'>({  'Status': '" + retStatus + "','Message': '" + retMsg +
                       "','UploadedPath': '" + retFilePath + "' })</pre>";
            this.Page.Form.Controls.Add(lit);
        }
        else
        {
            if (Request.Form["coOrdinates[x]"] != null)
            {
                try
                {
                    double x = double.Parse(Request.Form["coOrdinates[x]"].ToString());
                    double y = double.Parse(Request.Form["coOrdinates[y]"].ToString());
                    // int x2 = int.Parse(Request.Form["coOrdinates[x2]"].ToString());
                    // int y2 = int.Parse(Request.Form["coOrdinates[y2]"].ToString());
                    double     w           = double.Parse(Request.Form["coOrdinates[w]"].ToString());
                    double     h           = double.Parse(Request.Form["coOrdinates[h]"].ToString());
                    string     filePath    = Request.Form["filePath"].ToString();
                    int        storeID     = int.Parse(Request.Form["StoreID"].ToString());
                    int        portalID    = int.Parse(Request.Form["PortalID"].ToString());
                    string     cultureName = Request.Form["CultureName"];
                    string     path        = Server.MapPath("~/" + filePath);
                    FileStream fs          = new FileStream(path, FileMode.Open);

                    string newpath = AspxImageManagerController.CropBannerImage(fs, filePath,
                                                                                new AspxCommonInfo()
                    {
                        StoreID     = storeID,
                        PortalID    = portalID,
                        CultureName = cultureName
                    }, (int)h, (int)w, (int)x, (int)y);
                    retStatus   = 1;
                    retMsg      = "successfully cropped image!";
                    retFilePath = newpath;
                }
                catch (Exception)
                {
                    retStatus = -1;
                    retMsg    = "Failed to crop image!";
                }
            }

            string returnStr = "{  'Status': '" + retStatus + "','Message': '" + retMsg +
                               "','UploadedPath': '" + retFilePath + "' }";
            Response.Clear();
            Response.Write(returnStr);
        }
    }
Exemplo n.º 2
0
    protected void Page_Load(object sender, EventArgs e)
    {
        string         retMsg        = "fail";
        int            maxFileSize   = 0;
        int            retStatus     = -1;
        string         oldFile       = "";
        Random         rnd           = new Random();
        AspxCommonInfo aspxCommonObj = new AspxCommonInfo();

        const string path = "~/Modules/AspxCommerce/AspxItemsManagement/uploads/";

        if (Request.Form["DeleteImage"] != null)
        {
            if (Request.Form["OldFileName"] != null)
            {
                try
                {
                    oldFile = Request.Form["OldFileName"].ToString();
                    oldFile = Path.GetFileName(oldFile);
                    string largImage  = path + "Large/" + oldFile;
                    string midImage   = path + "Medium/" + oldFile;
                    string smallImage = path + "Small/" + oldFile;
                    string baseImage  = path + oldFile;
                    Thread.Sleep(2000);
                    if (File.Exists(HttpContext.Current.Server.MapPath(largImage)))
                    {
                        File.Delete(HttpContext.Current.Server.MapPath(largImage));
                    }
                    if (File.Exists(HttpContext.Current.Server.MapPath(midImage)))
                    {
                        File.Delete(HttpContext.Current.Server.MapPath(midImage));
                    }
                    if (File.Exists(HttpContext.Current.Server.MapPath(smallImage)))
                    {
                        File.Delete(HttpContext.Current.Server.MapPath(smallImage));
                    }
                    if (File.Exists(HttpContext.Current.Server.MapPath(baseImage)))
                    {
                        File.Delete(HttpContext.Current.Server.MapPath(baseImage));
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        else
        {
            if (Request.Form["MaxFileSize"] != null && Request.Form["MaxFileSize"] != "" &&
                int.Parse(Request.Form["MaxFileSize"].ToString()) > 0)
            {
                maxFileSize = int.Parse(Request.Form["MaxFileSize"].ToString());
            }
            if (Request.Form["StoreID"] != null)
            {
                aspxCommonObj.StoreID     = int.Parse(Request.Form["StoreID"].ToString());
                aspxCommonObj.PortalID    = int.Parse(Request.Form["PortalID"].ToString());
                aspxCommonObj.CultureName = Request.Form["CultureName"].ToString();
            }
            string strFileName = Path.GetFileName(HttpContext.Current.Request.Files[0].FileName);
            int    strSize     = HttpContext.Current.Request.Files[0].ContentLength;

            string mapPath  = HttpContext.Current.Server.MapPath(path);
            string tempPath = HttpContext.Current.Server.MapPath("~/Upload/temp/");
            if ((strSize > 0) && (strSize < maxFileSize * 1024))
            {
                HttpPostedFile uploadedfile = HttpContext.Current.Request.Files[0];
                string         filename     = uploadedfile.FileName;
                string         strExtension = Path.GetExtension(filename).ToLower();
                filename = filename.Substring(0, filename.Length - strExtension.Length);
                filename = filename + '_' + rnd.Next(111111, 999999).ToString() + strExtension;

                uploadedfile.SaveAs(tempPath + "\\" + filename);
                FileStream fs = new FileStream(tempPath + "\\" + filename, FileMode.Open);
                AspxImageManagerController.AddWaterMarksCostVariantItem(fs, path, filename,
                                                                        aspxCommonObj);
                retMsg    = "Modules/AspxCommerce/AspxItemsManagement/uploads/" + filename;
                retStatus = 1;
            }
            else
            {
                retMsg = "Sorry, the image must be less than " + maxFileSize + "KB";
            }
            HttpContext.Current.Response.ContentType = "text/plain";
            HttpContext.Current.Response.Write("({ 'Status': '" + retStatus + "','Message': '" + retMsg + "' })");
            HttpContext.Current.Response.End();
        }
    }