Tuesday, 10 June 2014

File Upload with ASP.NET | How to Use FileUpload Control in ASP.Net Using C#.Net,VB.Net | Upload File in Asp.net and Save in Folder

6/10/2014 - By Pranav Singh 6

In this article I will show you how you can upload the file in asp.net using fileupload control. Here I will show you to upload and image file to the to the server and display it’s preview in image control.


So for this article first we will create a new asp.net application and asp.net application and add a fileupload control and a button control.
After adding all the controls our page code will look as shown below:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FileUploadControl.aspx.cs"
    Inherits="ProjectDemo_Asp.et.FileUploadControl" %>

<!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 Upload with ASP.NET | How to Use FileUpload Control in ASP.Net Using C#.Net</title>
    <style type="text/css">
 p.MsoNormal
       {margin-top:0in;
       margin-right:0in;
       margin-bottom:10.0pt;
       margin-left:0in;
       line-height:115%;
       font-size:11.0pt;
       font-family:"Calibri","sans-serif";
       }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    </div>  
            <asp:FileUpload ID="FileUpload1" runat="server" />
            &nbsp;<asp:Button ID="Button1" runat="server" Text="Upload file"
                onclick="Button1_Click" />
            <br />
            <br />
            <asp:Label ID="lblmessage" runat="server" Style="color: #FFFFFF; font-weight: 700;
                background-color: #FF6699" Text=""></asp:Label>
    <p class="MsoNormal" style="text-decoration: underline">
        <strong>Preview</strong></p>
    <p class="MsoNormal" style="text-decoration: underline">
        <asp:Image ID="Image1" runat="server" Height="181px" Width="184px" />
    </p>
    </form>
</body>
</html>

Now generate the click event of the button control and add the below code

C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

        }
        /// <summary>
        /// Button click event to upload the selcted file
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string filename = System.IO.Path.GetFileName(FileUpload1.FileName);
                FileUpload1.SaveAs(Server.MapPath("~/Folder/") + filename);
                /*Uploaded file path*/
                string filePath = "Folder/" + filename;
                /*******************************************/
                /*Code to save the file path into data base*/
                /*******************************************/
                lblmessage.Text = "File uploaded successfully.";
                Image1.ImageUrl = filePath;
            }
            else
            {
                lblmessage.Text = "Please select file.";
            }
        }
    }
}

VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Namespace ProjectDemo_Asp.et
    Partial Public Class FileUploadControl
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        End Sub
        ''' <summary>
        ''' Button click event to upload the selcted file
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
            If FileUpload1.HasFile Then
                Dim filename As String = System.IO.Path.GetFileName(FileUpload1.FileName)
                FileUpload1.SaveAs(Server.MapPath("~/Folder/") + filename)
                'Uploaded file path

                Dim filePath As String = "Folder/" + filename
                '*****************************************

                'Code to save the file path into data base

                '*****************************************

                lblmessage.Text = "File uploaded successfully."
                Image1.ImageUrl = filePath
            Else
                lblmessage.Text = "Please select file."
            End If
        End Sub
    End Class
End Namespace

In above code first we have written to get the file name and after that we have saved the file name in our file directory. After that we can write the code save the code into data base.

Now we have done run the code to view the output. Now select the file and click on upload button. You will get the selected file in the code side as shown below.


Final output.



DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

6 comments:

  1. Fantastic!
    Has to be the first time I copy and paste code from the internet and works first time!
    Nice and easy, very well explained. Well done!
    Just a question, seems like there is a limitation on the file size to upload...any ideas how to allow big size files to be uploaded ?
    Again, thanks so much for sharing.

    ReplyDelete
    Replies
    1. Hi thanks for your comment. for uploading large file. u need to do some configration setting. have a look of this link

      http://blogs.msdn.com/b/prashant_upadhyay/archive/2011/07/13/large-file-upload-issue-in-asp-net.aspx

      Delete
  2. Access to the path 'e:\HostingSpaces\maro2008\aboazez.com\wwwroot\Folder\111.gif' is denied.

    please help me

    ReplyDelete
    Replies
    1. If you are giving physical path then you will get the error. you need to add a folder in root of the project and while uploading the file server.mappath.

      if this still this solution not work . Please provide the code you are using.

      Delete
    2. close the door do your work properly

      Delete
  3. If you are giving physical path then you will get the error. you need to add a folder in root of the project and while uploading the file server.mappath.

    if this still this solution not work . Please provide the code you are using.

    ReplyDelete

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top