My Professional Profile

I am Johnson Augustine Sr.Software Engineer and System Architect. I have 10 Yrs of hands on expertise in ASP.net MVC 5 , Angular 5 , C# MVC Razor, WPF MVVM , Android , IOS Swift 3 , IOS11 MSSQL,MySQL Database,,PHP,C/C++/Visual C++/G++/QT++,Com,DirectX,Open CV,EMGU CV , embedded System Development , [Raspberry PI]. ,html,Javascript,Jquery,Ajax.CSS , Networking ,Cyber security, Ethical Hacking You can see my professional profile at http://linkedin.com/in/johnsontaugustine Email : johnsonaugustine@live.com

Wednesday, 1 May 2013

Uploading files in asp.net c#


//Wrtite FileUpload.aspx

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

<!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>Upload Image</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="UploadControl" runat="server" />
        &nbsp;
        <asp:Button ID="btnUpload" runat="server" Text="Upload"
            onclick="btnUpload_Click" />
        <br />
        <br />
        <br />
        <asp:Image ID="imgViewr" runat="server" />
        <span id="Error" runat="server"></span>
    </div>
    </form>
</body>
</html>


FileUpload.cs

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class FileUpload : System.Web.UI.Page
{
    public string strImagePath ="~/Images/";  
    public System.Collections.Generic.List<string> arrValidExtention = new System.Collections.Generic.List<string>();
    protected void Page_Load(object sender, EventArgs e)
    {
        arrValidExtention.Add(".JPG");
        arrValidExtention.Add(".JPEG");      
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {  
        //Check file is selected or not
        if (UploadControl.PostedFile.FileName != "")
        {
            try {
                //Get selected file extention
                string strFileExtention = System.IO.Path.GetExtension(UploadControl.PostedFile.FileName).ToUpper();
                //if (strFileExtention == ".jpg".ToUpper() || strFileExtention == ".jpeg".ToUpper())                
               
                if (arrValidExtention.Contains(strFileExtention))              
                {
                    //Save selected file
                    UploadControl.PostedFile.SaveAs(Server.MapPath(strImagePath + UploadControl.PostedFile.FileName));
                    Error.InnerText = "File uploaded successfully.";
                }
                else
                {
                    Error.InnerText = "Please select JPEG|JPG file.";
                }
            }
            catch (Exception exp)
            {
                Error.InnerText = exp.Message.ToString();
            }
        }
    }

   
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.