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.
So of my articles related to file upload are as follows,
please check Ajax
FileUpload Control In Asp.Net or Multiple FileUpload With Progress Example in
Asp.Net Using C#.Net, 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, How
To Get File Size Uploaded By FileUpload Control Using C# In Asp.Net.
Some other articles are as follows: File
Upload in C#.Net Windows Application, Accordion
Ajax Control Toolkit Example in Asp.net OR How to Use Ajax Accordion Control in
Asp.Net, RadioButtonList
Validation Using jQuery in Asp.Net, Asp.Net-Validating
Radio Button List using Required Filed Validator | How to Use Required Field
Validator For Radiobuttonlist in Asp.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" />
<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
Protected Sub btnUpload_Click(ByVal sender As Object, ByVal e As EventArgs)
ReplyDeleteDim fileName As String = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)
FileUpload1.PostedFile.SaveAs(Server.MapPath(("~/Uploads/" + fileName)))
End Sub