connectseekers.com

Thursday, 12 June 2014

How to Get the Full Path of FileUpload Control in Asp.Net

6/12/2014 - By Pranav Singh 0

In this article I will show you how you can get the file uploaded  path In your asp.net application using c#.Net and VB.net.



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

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

<!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>How to Get the Full Path of FileUpload Control in Asp.net</title>
</head>
<body>
    <form id="form1" runat="server">
    <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>
    </div>
    </form>
</body>
</html>

Now in your .cs page we will 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 GetUpoadedFileDetail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
           if (FileUpload1.HasFile)
            {
             string filename=System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
             FileUpload1.SaveAs(Server.MapPath("Images/" + filename));
             string filepath = "Images/" + filename;
             lblmessage.Text = "File Uploaded Path is : " + 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 GetUpoadedFileDetail
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        End Sub

        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.PostedFile.FileName)
                FileUpload1.SaveAs(Server.MapPath("Images/" + filename))
                Dim filepath As String = "Images/" + filename
                lblmessage.Text = "File Uploaded Path is : " + filepath
            Else
                lblmessage.Text = "Please select file."
            End If
        End Sub
    End Class
End Namespace

In above code we will be able to find out what is the uploaded file path when you are using fileupload control in your asp.net page.

Now run the page to get the 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

0 comments:

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
Email Id : contact@aspdotnet-pools.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


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top