Thursday 20 February 2014

Import Bulk Images from Client to server using asp.net with IONIC.Zip

 public void ImportImages(string path, string fname)
    {
        try
        {
           
            using (ZipFile zip1 = ZipFile.Read(path))
            {
                zip1.ExtractAll(Server.MapPath("~/UserImages"), ExtractExistingFileAction.OverwriteSilently);
            }
                string sdir = path.Substring(0, path.IndexOf('.'));
                string destDirName = Server.MapPath("~/UserImages/");
                DirectoryInfo dir = new DirectoryInfo(sdir);
                FileInfo[] files = dir.GetFiles();
                foreach (FileInfo file in files)
                {
                    String ext = System.IO.Path.GetExtension(file.FullName);
                    if (ext.ToLower() == ".jpg" || ext.ToLower() == ".png" || ext.ToLower() == ".gif" || ext.ToLower() == ".bmb")
                    {
                        string temppath = Path.Combine(destDirName, file.Name);
                        file.CopyTo(temppath, true);
                    }
                }
                //delete file
                if (File.Exists(path))
                {
                    File.Delete(path);
                }
                //delete directory
                if (Directory.Exists(sdir))
                {
                    Directory.Delete(sdir,true);
                }
           
        }
        catch (Exception ex)
        {
            string mmsg = ex.Message;
            showEXPMessages("(ImportImages)  " + mmsg); showMessages(mmsg);
       
        }
    }

Create Zip and download file using IONIC.ZIP in asp.net

 public void crateZip()
    {
        FileStream fs = null;//, fs2=null;
        try
        {
        string  strFName= Session["FileName"]!=null?Session["FileName"].ToString():"";
        string destination=  Session["FileLocation"] != null?Session["FileLocation"].ToString():"";
        string filep = Request.PhysicalApplicationPath + "FolderName//";
        string datafolder =filep+ strFName.Substring(0, strFName.Length-4);
        string foldername=strFName.Substring(0, strFName.Length - 4);
        string path = Server.MapPath("~/FolderName/" + foldername+"/");
        string[] filenames = Directory.GetFiles(path);
        using (ZipFile zip = new ZipFile())
        {
            zip.AddFile(destination,"");
            zip.AddFiles(filenames, foldername);
            zip.Save(Server.MapPath("~/" + foldername + ".zip"));        
        }


        string zipFullPath = Server.MapPath("~/" + foldername + ".zip");
     
        string strPath;
        strPath = Session["FileLocation"].ToString();
        string strFileName;
        strFileName = Session["FileName"].ToString();

            //fs=new FileStream();
            fs = File.Open(zipFullPath, FileMode.Open);
            byte[] bytBytes = new byte[(fs.Length)];
            fs.Read(bytBytes, 0, (int)fs.Length);
            fs.Close();
            Response.AddHeader("Content-disposition", "attachment; filename=" + foldername + ".zip");
            Response.ContentType = "application/octet-stream";
            Response.BinaryWrite(bytBytes);

            if (File.Exists(strPath))
            {
                System.IO.File.Delete(strPath);
            }
            if (File.Exists(zipFullPath))
            {
                System.IO.File.Delete(zipFullPath);
            }
            if (Directory.Exists(path))
            {
                Directory.Delete(path,true);          
            }
            Response.Flush();
            Response.End();
        }

        catch (System.Exception ex)
        {
       
        }
        finally { fs.Dispose(); Response.Clear();}

    }

Export images from web to Client

 public void CopyFiles(string sourcePath, string destinationPath, string sFileName)
    {
        try
        {
            string Sfilepthe = sourcePath + "\\" + sFileName;
            string Sdestination = destinationPath + "\\" + sFileName;
            if (File.Exists(Sfilepthe))
            {
                File.Copy(Sfilepthe, Sdestination, true);
            }
        }
        catch (Exception ex)
        {
            string mmsg = ex.Message;
            showEXPMessages("(CopyFiles)  " + mmsg);// showMessages(mmsg);
        }
    }

Check Image size and type using javascript in asp.net

 function GetFileSizetypeIMG() {
    var maxsize=2*1024*1024//1mb=1*1024*1024
        var aspFileUpload = document.getElementById("<%=Upload1.ClientID%>");
        var filesze = aspFileUpload.files[0].size;
        var fileName = aspFileUpload.value;
        var ext = fileName.substr(fileName.lastIndexOf('.') + 1).toLowerCase();
        if (!(ext == "zip")) {
            alert("Invalid file type, must select a (*.zip) file");
            return false;
        }
        if (filesze > maxsize) {
            alert("Selected file not more than " + maxsize/(1024*1024)+"MB");
            return false;
        }
        return false;
    }

Friday 7 February 2014

Copy Images from one to another folder using c# or asp.net

Create Directory-
string diretorypath= Application.StartupPath;
string ImagePath = diretorypath+ "\\" + "Images_" + DateTime.Now.ToString("ddMMyyyy") + "_" + DateTime.Now.ToString("hhmmss");

if(!Directory.Exists(Sfilepthe))//create directory
                Directory.CreateDirectory(ImagePath);


//Copy files

 public void CopyFiles(string sourcePath, string destinationPath, string sFileName)
        {
            try
            {
                string Sfilepthe = sourcePath + "\\" + sFileName;
                string Sdestination = destinationPath + "\\" + sFileName;
                if (File.Exists(Sfilepthe))
                {
                    File.Copy(Sfilepthe, Sdestination, true);
                }
            }
            catch (Exception ex)
            { }
        }

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...