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();
    }
}



Related Posts:

  • SQL - Date and Time Functions Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4 … Read More
  • SQL SQLObjective type Questions SQLObjective type Question and Answers Set 1  Click here 1.       What does SQL stand for? a.         Structured Query Language b. &… Read More
  • Fuzzy logic Fuzzy logic Fuzzy logic is an approach to computing based on "degrees of truth" rather than the usual "true or false" (1 or 0) Boolean logic on which the modern computer is based. Example Fuzzy set theory defines fuzzy ope… Read More
  • Difference between Store Procedure and Trigger? Difference between Store Procedure and Trigger? We can call stored procedure explicitly. But trigger is automatically invoked when the action defined in trigger is done.      ex: create trigger aft… Read More
  • DataReader vs DataSet DataReader        I.            DatReader works on a Connection oriented architecture.     II.    &nb… Read More

0 comments:

Post a Comment