File Upload


Below is the simple code to upload file to server. First create a folder with name 'UploadedImages'.

ASPX Code
----------------------------------------------------------------------------------------------

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

<!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>File Uploader</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblImage" runat="server" Text="Image"></asp:Label>
        <asp:FileUpload ID="flupdImage" runat="server" />
       
        <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
       
        <br />
        <br />
        <asp:Button ID="btnUpload" runat="server" Text="Upload"
            onclick="btnUpload_Click" />
            <hr />
            <br />
        <asp:Image ID="imgUploaded" runat="server" Height="175px" Width="156px" />
    </div>
    </form>
</body>
</html>

.CS Code
----------------------------------------------------------------------------------------------


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (flupdImage.HasFile)
        {
            if (CheckExtension(flupdImage.FileName))
            {
                if (flupdImage.PostedFile.ContentLength < 1024000)
                {
                    string path = Server.MapPath("UploadedImages//" + flupdImage.FileName);
                    flupdImage.SaveAs(path);
                    imgUploaded.ImageUrl = "~/UploadedImages/" + flupdImage.FileName;
                    lblMessage.Text = "Image uploaded successfully.";

                    /* To save image path in database
                     * 
                     *  string imgPath = "~/" + flupdImage.FileName;
                     *  and save imgPath in database.
                     *  when retriving imgPath from database bind the path with image control of .net not in <img> of html as
                     *  the HTML do not understand ~ (root).
                     *
                     */
                }
                else
                {
                    lblMessage.Text = "Maximum size 1MB allowed.";
                }
            }
            else
            {
                lblMessage.Text = "Only .jpg or .gif images are allowed.";
            }
        }
        else
        {
            lblMessage.Text = "Please select image..";
        }
    }

    public bool CheckExtension(string fileName)
    {
        string extension = Path.GetExtension(fileName);

        switch (extension.ToLower())
        {
            case ".jpg": return true;
                break;
            case ".gif": return true;
                break;
            case ".jpeg": return true;
                break;
            default: return false;
                break;
        }
    }
}





Comments

Post a Comment

Popular posts from this blog

Get Query String Values With Javascript

Change Css Class of li in ul onclick

Change Text Color with Javascript