Upload images and show the list of image in gridview to delete-asp.net code

Below code will upload image in a particular folder and will show the list of images from that folder. User may delete images from that list.

HTML Code:

<div>
        <table style=”width: 90%”>
            <tr>
                <td style=”width: 50%”>
                    File Upload with Thumbnail Generator:<br />
                    <asp:FileUpload ID=”FileUpload1″ runat=”server” /><br />
                    <asp:Button ID=”buttonUpload” runat=”server” Text=”Upload” OnClick=”buttonUpload_Click” /><br />
                </td>
                <td style=”width: 50%”>
                    <asp:GridView ID=”UploadedFiles” DataSource=”<%# GetUploadList() %>” runat=”server”
                        CellPadding=”4″ ForeColor=”#333333″ GridLines=”None” OnRowDeleting=”UploadedFiles_RowDeleting”
                        AutoGenerateColumns=”False”>
                        <Columns>
                            <asp:TemplateField HeaderText=”File”>
                                <ItemStyle Width=”100%” />
                                <ItemTemplate>
                                    <asp:HyperLink ID=”FileLink” NavigateUrl='<%# “~/Uploads/” + Container.DataItem.ToString() %>’
                                        Text='<%# Container.DataItem.ToString() %>’ runat=”server” />
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:CommandField ButtonType=”Image” DeleteImageUrl=”~/media/delete.gif” ShowDeleteButton=”True”>
                                <ItemStyle Width=”1px” />
                            </asp:CommandField>
                        </Columns>
                        <FooterStyle BackColor=”#507CD1″ Font-Bold=”True” ForeColor=”White” />
                        <RowStyle BackColor=”#EFF3FB” />
                        <EditRowStyle BackColor=”#2461BF” />
                        <SelectedRowStyle BackColor=”#D1DDF1″ Font-Bold=”True” ForeColor=”#333333″ />
                        <PagerStyle BackColor=”#2461BF” ForeColor=”White” HorizontalAlign=”Center” />
                        <HeaderStyle BackColor=”#507CD1″ Font-Bold=”True” ForeColor=”White” />
                        <AlternatingRowStyle BackColor=”White” />
                    </asp:GridView>
                </td>
            </tr>
        </table>
    </div>


C# Code:

    override protected void OnLoad(System.EventArgs e)
    {
        base.OnLoad(e);
        if (!IsPostBack)
        {
            UploadedFiles.DataBind();
        }
    }

    protected string[] GetUploadList()
    {
        string folder = Server.MapPath(“~/Uploads”);
        string[] files = Directory.GetFiles(folder);
        string[] fileNames = new string[files.Length];
        Array.Sort(files);

        for (int i = 0; i < files.Length; i++)
        {
            fileNames[i] = Path.GetFileName(files[i]);
        }

        return fileNames;
    }

    protected void buttonUpload_Click(object sender, System.EventArgs e)
    {
        System.Drawing.Image image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);

        string fileName = Path.Combine(Server.MapPath(“~/uploads”), FileUpload1.FileName);
        if (File.Exists(fileName))
            File.Delete(fileName);
        image.Save(fileName);

        float imgWidth = image.PhysicalDimension.Width;
        float imgHeight = image.PhysicalDimension.Height;
        float imgSize = imgHeight > imgWidth ? imgHeight : imgWidth;
        float imgResize = imgSize <= 128 ? (float)1.0 : 128 / imgSize;
        imgWidth *= imgResize; imgHeight *= imgResize;
        System.Drawing.Image thumb = image.GetThumbnailImage((int)imgWidth, (int)imgHeight, delegate() { return false; }, (IntPtr)0);

        fileName = Path.Combine(
        Server.MapPath(“~/uploads”),
        string.Format(“{0}_th{1}”,
        Path.GetFileNameWithoutExtension(FileUpload1.FileName),
        Path.GetExtension(FileUpload1.FileName)
        )
        );
        if (File.Exists(fileName))
            File.Delete(fileName);

        thumb.Save(fileName);

        UploadedFiles.DataBind();
    }

    protected void UploadedFiles_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        e.Cancel = true;
        string fileName = ((HyperLink)UploadedFiles.Rows[e.RowIndex].FindControl(“FileLink”)).Text;
        fileName = Path.Combine(Server.MapPath(“~/uploads”), fileName);
        File.Delete(fileName);
        UploadedFiles.DataBind();

    }

Output Screen:

image upload and list of images


Leave a Comment

RSS
YouTube
YouTube
Instagram