Friday, 30 March 2012

MOVE , REPLACE And DELETE Images File Dynamically In ASP.Net

Move,Copy,Save,Replace And Delete Image File Dynamically In ASP.Net


To maintain your server memory with saving files.you must sure the unused file should be deleted,means if Record updated then old file should be replace by new one,and at the time of client choosing file then it should place under temporary folder and continuously  replace old file with new file.don't worry if you didn't understand above theory.Let do it Practically.
To remind you i've already explained How To Delete File From Server  ,And here we are doing totally different things with file on server


 

1)As this is as File input- output process you must add the namespace

using System.IO;



  2)On Clicking Upload Button And Save file In Temporary Folder

protected void ButtonUpload_Click(object sender, EventArgs e)
    {

        if (FileUpload1.HasFile)
        {
           FileInfo TheFile = new FileInfo(MapPath(".") + "\\" + "itemimages/tempImages/" + "Item" + TextBoxItemCode.Text + ".jpg");
            if (TheFile.Exists)
            {
                File.Delete(MapPath(".") + "\\" + "itemimages/tempImages/" + "Item" + TextBoxItemCode.Text + ".jpg");
            }
           

            FileUpload1.SaveAs(Server.MapPath("~/itemimages/tempImages/") + "Item" + TextBoxItemCode.Text + ".jpg");
            ImageLogo.ImageUrl = "~/itemimages/tempImages/" + "Item" + TextBoxItemCode.Text + ".jpg";


        }
        else
        {
            CloseWindow = "alert('At First Browse and Select Item Image');";
            ScriptManager.RegisterStartupScript(this.up, this.up.GetType(), "CloseWindow", CloseWindow, true);
        }
    
    }







3)
TextBoxItemCode.Text:-unique name for image

"~/itemimages/tempImages/":-Path for file after clicking update button not save or update.

"~/itemimages/ItemImages/":-Path,which will finally save or updated and use to save in database.

+ "Item" +:- Just to make sure every image will save with prefix Item








4)
On save Button Click
 means
if(ButtonSave.Text=="Save")



string destinationFile ;
 string images = ImageLogo.ImageUrl;
 if (CheckBoxLogo.Checked == true)
 {
         images = "";
 }
 else
{
 string sourceFile = Server.MapPath("~/itemimages/tempImages/" + "Item" + TextBoxItemCode.Text + ".jpg");
 destinationFile = Server.MapPath("~/itemimages/ItemImages/" + "Item" + TextBoxItemCode.Text + ".jpg");
   System.IO.File.Move(sourceFile, destinationFile);

}









5)
On Update Button Click
 means
 else if(ButtonSave.Text=="Update")


string images = ImageLogo.ImageUrl;
 if (CheckBoxLogo.Checked == true)
{
       images = "";
}
  else
 {
        images = ImageLogo.ImageUrl;
         if (CheckBoxLogo.Checked == true)
         {
                            images = "";
          }
          else
          {
    string sourceFile = Server.MapPath("~/itemimages/tempImages/" + "Item" + TextBoxItemCode.Text + ".jpg");
    destinationFile = Server.MapPath("~/itemimages/ItemImages/" + "Item" + TextBoxItemCode.Text + ".jpg");
    FileInfo TheFile = new FileInfo(MapPath( "~/itemimages/ItemImages/" + "Item" + TextBoxItemCode.Text + ".jpg"));
     if (TheFile.Exists)
       {
                    File.Delete(MapPath("~/itemimages/ItemImages/" + "Item" + TextBoxItemCode.Text + ".jpg"));
         }   
         System.IO.File.Move(sourceFile, destinationFile);
                               
      }
                 

   }





6)Required TABLE With Above Code


CREATE TABLE [dbo].[tb_Item](
    [Item_Id] [bigint] IDENTITY(1,1) NOT NULL,
    [Item_Code] [varchar](100) NULL,
    [Item_Name] [varchar](200) NULL,
    [Qty] [float] NULL,
    [QtyUnit] [varchar](50) NULL,
    [InvLevel] [float] NULL,
    [Remarks] [varchar](max) NULL,
    [ItemImage] [varchar](max) NULL
)






and here is what you're lookin for

Apart From Only this I am Also Giving You Code By Which You Can Not Only Move File OVER server but can do copy paste job too,Hope you like this too.



 protected void Button_Click(object sender, EventArgs e)
    {
        string sourceFile = Server.MapPath("Data.txt");
        string destinationFile = Server.MapPath("Datacopy.txt");
        System.IO.File.Copy(sourceFile, destinationFile);
    }






2 comments:

Print Only Grid View in ASP.net

ASP.net How to Print Only GridView < div id ="gridviewDiv" >   < asp:GridView ID ="gridViewToPri...