In Asp.net How to upload Image In to server

In Asp.net How to upload Image In to server:


In Asp.net How to upload Image In to server?  Is a major task, in this Post we learn this. File upload control in asp.net programming is a compilation of two controls of asp.net. One is a Text Box and second is a button control. Basically this control gives the facility to save the image in to the server by user. In programming Many time user want to use this control for ‘Inserting Image of user, Uploading Resume (CV), Uploading PDF file to server, and many kind of work.






Code : for  : In Asp.net How to upload Image In to server  Aspx page :-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CS.aspx.cs" Inherits="CS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>

<body>
    <form id="form1" runat="server">
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
    <hr />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowHeader="false">
        <Columns>
            <asp:BoundField DataField="Text" />
            <asp:ImageField  DataImageUrlField="Value" ControlStyle-Height="100" ControlStyle-Width="100" />
        </Columns>
    </asp:GridView>
    </form>
</body>
</html>


Code for  : In Asp.net How to upload Image In to server   aspx.cs page


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

public partial class CS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string[] filePaths = Directory.GetFiles(Server.MapPath("~/Images/"));
            List<ListItem> files = new List<ListItem>();
            foreach (string filePath in filePaths)
            {
                string fileName = Path.GetFileName(filePath);
                files.Add(new ListItem(fileName, "~/Images/" + fileName));
            }
            GridView1.DataSource = files;
            GridView1.DataBind();
        }
    }
    protected void Upload(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile)
        {
            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }
}

Related Questions:

Qus 5: How to use File Upload control.

Qus 6: How to upload a file to a Web server in ASP.NET PROGRAMMING by using.

Qus 7: Uploading files to a web server with Asp.net Programming.

Qus 8: Uploading and Displaying Images from Database Using asp.net Programming.

Qus 9: Upload Image To Web Server In ASP.NET PROGRAMMING.

Qus 10: Asp net image upload.

Qus 11: ASP.NET PROGRAMMING File Upload Control.

Qus 12: Learn How to Upload Images in Your ASP.NET PROGRAMMING Website.

Qus 13: Upload images to folder and display uploaded images in.

Popular posts from this blog