Friday 11 October 2013

Creating new Folder and Upload Image on Server

Creating new Folder and Upload Image on  Server
---------------------------------------------------------------------------------------------------------  
.aspx pages
---------------------------------------------------------------------------------------------------------  

 <table style="text-align: center" width="100%">
                        <tr>
                            <td align="left">
                                <br />
                                Upload picture :
                                <asp:FileUpload ID="fileuploadPic" runat="server" CssClass="flpUploadCss" />
                                <br />
                            </td>
                        </tr>
                        <tr>
                            <td align="center">
                                <br />
                                <center>
                               
                                    <br />
                                    <asp:Button ID="btnchangepic" runat="server" Text="Upload" OnClick="btnchangepic_Click"
                                        CssClass="btnblu_new" Height="25px" Width="80px" />
                                </center>
                            </td>
                        </tr>
</table>



---------------------------------------------------------------------------------------------------------  
.cs  code 
--------------------------------------------------------------------------------------------------------- 
protected void btnchangepic_Click(object sender, EventArgs e)
        {
            try
            {
                bool flag = true;
                if (fileuploadPic.HasFile)
                {
                    string fileExt = Path.GetExtension(fileuploadPic.FileName);
                    if (fileExt == ".jpeg" || fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png")
                    {
                        flag = true;
                    }
                    else
                    {
                        flag = false;
                        // alert(this, "Upload only - .jpeg,.jpg,.gif,.png files", "");
                    }
                }
                if (flag)
                {
    if (fileuploadPic.HasFile)
                    {
                        long maxFileSize = 4216380;
                        string filename = fileuploadPic.FileName.Replace("'", "''");
             string strPath = Server.MapPath("~/UserImages");  / / it's folder name  on server in which                                    new directory is created

               

                        strPath += "\\" + oDC_User.FName + "-" + oDC_User.UserId;

                        if (!Directory.Exists(strPath))
                        {
                            Directory.CreateDirectory(strPath);
                        }

                        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(strPath);
                        foreach (FileInfo file in dir.GetFiles())
                        {
                            file.Delete();
                        }
                        if (fileuploadPic.PostedFile.ContentLength <= maxFileSize)
                        {
                            strPath = strPath + "\\" + filename;
                            fileuploadPic.SaveAs(strPath);
                        }
                 
                    }
   }
   }
            catch (Exception ex)
            {
           
                }
               
            }
        }

No comments:

Post a Comment