Upload file or Image and save in a folder and Display in Grid View

Default.aspx:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="image_upload.aspx.cs" Inherits="image_upload" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
        <asp:FileUpload ID="FileUpload1" runat="server" Width="292px" />
&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" Text="Upload" OnClick="upload"/>
        <br />
        <br />
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ShowHeader="False" CellPadding="4" ForeColor="#333333" GridLines="None">
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
    <Columns>
        <asp:BoundField DataField="Text" />
        <asp:ImageField DataImageUrlField="Value" ControlStyle-Height="100" ControlStyle-Width="100" >
<ControlStyle Height="100px" Width="100px"></ControlStyle>
        </asp:ImageField>
    </Columns>
            <EditRowStyle BackColor="#999999" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#E9E7E2" />
            <SortedAscendingHeaderStyle BackColor="#506C8C" />
            <SortedDescendingCellStyle BackColor="#FFFDF8" />
            <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
 
    </div>
    </form>
</body>
</html>


Default.aspx.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class image_upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            gridBind();
        }
    }
    private void gridBind()
    {
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/Image/"));
        List<ListItem> files = new List<ListItem>();
        foreach (string filePath in filePaths)
        {
            string fileName = Path.GetFileName(filePath);
            files.Add(new ListItem(fileName, "~/Image/" + fileName));
        }
        GridView1.DataSource = files;
        GridView1.DataBind();
    }
    protected void upload(object sender, EventArgs e)
    {
        /* This code is also good and executable don't worry about this*/
        //if (FileUpload1.HasFile)
        //{
        //    string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
        //    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Image/") + fileName);
        //    Response.Redirect(Request.Url.AbsoluteUri);
        //}

        /* Below these two code lines are used to store the image files Inside the folder with Unique Names 
         Here Guid is used to Generate random names not duplicate every name is unique*/
        String sp = Server.MapPath("~/Image/");
        String fn = Guid.NewGuid().ToString() + FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."));
        /* Below these two code lines are used to store the image files outside the folder
         Here dot(.) is used for current folder */
        //String sp = Server.MapPath(".");
        //String fn = FileUpload1.FileName;
        
        if(sp.EndsWith("\\")==false)
        {
            sp+="\\";
        }
        sp += fn;
        FileUpload1.PostedFile.SaveAs(sp);
        Response.Write("File Uploaded Successfully into images folder");
        gridBind();
    }
}



0 comments:

Post a Comment