[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1810
  • Last Modified:

Get file path on client's machine

If a website is hosted on server

If I select the file from client's machine

How can I get the path of file on client's machine
0
KaranGupta
Asked:
KaranGupta
  • 3
1 Solution
 
Muhammad Ousama GhazaliSolution Analyst & ArchitectCommented:
Due to security risks, you don't get the path of the file at the client's machine. However, the file upload control (HTML or ASP.NET based) allows to you upload the file from the client into server using server-side code.

For non-ASP.NET web site, you must include values for action and enctype attributes of the form element in HTML. For reference, please see this link http://www.w3schools.com/tags/att_form_enctype.asp

In ASP.NET you can very easily use the FileUpload control as follows in ASPX file:

<asp:FileUpload ID="FileUpload1" runat="server" />

Use the code snippet provided at the code behind file on some event. The code sample here not only checks the availability of the file but also it's size, type, etc. before saving it.

For official documentation on FileUpload control, please see: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx

Hope this helps.
If FileUpload1.HasFile AndAlso FileUpload1.PostedFile IsNot Nothing Then
 
	If FileUpload1.PostedFile.ContentLength > 0 Then
 
		Select Case FileUpload1.PostedFile.ContentType
			Case "image/pjpeg", "image/jpeg", "image/gif"
				If _
				 (FileUpload1.PostedFile.FileName.Length <> 0 And FileUpload1.PostedFile.ContentLength = 0) Or _
				 (FileUpload1.PostedFile.ContentLength > 1048576) Then
					'if file name is not empty or contents are not null then file size should be > 0 and < 1MB
					'Write code here to display appropriate error message that file size is not supported
 
				Else
					FileUpload1.PostedFile.SaveAs(Server.MapPath(".") & "\Uploads\" & FileUpload1.PostedFile.FileName)
 
				End If
 
			Case Else
				'Write code here to display appropriate error message saying file type is not supported
 
		End Select
 
	End If
 
Else
	'no file's been found for upload
	'Write code here to display appropriate error message 
 
End If

Open in new window

0
 
tcullerCommented:
The above works in VB.NET, but I'm assuming you want it in C# (hence, the presence of it within 2 C# Zones). I'll translate it to C# for you, in case you're not familiar with VB. The code is pretty much identical, though--I didn't take the time to check for any deep-set errors.

Hope this helps,
Nate

public void UploadFiles()
{
    if (FileUpload1.HasFile && FileUpload1.PostedFile != null && FileUpload1.PostedFile.ContentLength > 0)
    {
        switch (FileUpload1.PostedFile.ContentType)
        {
            case "image/pjpeg":
            case "image/jpeg":
            case "image/gif":
                if ((FileUpload1.PostedFile.FileName.Length != 0 && FileUpload1.PostedFile.ContentLength == 0)
                    || FileUpload1.PostedFile.ContentLength > 1048576)
 
                {
                    // Write code to handle if the File's name is not empty,
                    // or if contents or null, or if the content is greater than 1 MB~
                }
                else
                {
                    FileUpload1.PostedFile.SaveAs(Server.MapPath(".") + "\\Uploads\\" + FileUpload1.PostedFile.FileName);
                }
                break;
 
            default:
                // Write code for unrecognized file-type
                break;
        }
    }
    else
    {
        // No files found -- Error message.
    }
}

Open in new window

0
 
Muhammad Ousama GhazaliSolution Analyst & ArchitectCommented:
You can always use language converters especially if the languages are natively supported by .NET Framework.

One of the good converters for C# to VB.NET and vice versa are:

C# to VB.NET
http://www.developerfusion.com/tools/convert/csharp-to-vb/

VB.NET to C#
http://www.developerfusion.com/tools/convert/vb-to-csharp/
0
 
KaranGuptaAuthor Commented:
Hi

I have used the following code
if (txtSelectVideo.PostedFile.FileName.Trim() != string.Empty)
            {                
                txtSelectVideoOnPostback.Text = txtSelectVideo.PostedFile.FileName;
               
            }
Now let us consider the path is c:\test\karan.wmv

But what  I am getting in txtSelectVideoOnPostback. textbox is karan.wmv, but instead it should show the complete path

When the run this code on my local machine it works well

But when I upload the code on the staging server I only see the file name instead of full path
0
 
Muhammad Ousama GhazaliSolution Analyst & ArchitectCommented:
As I wrote earlier due to security reasons and risks involved for potentially harmful scripts to access client machines, it is not possible to have the fully qualified path of the file at the "server end" that essentially is your ASP.NET code. Whether you run your code on your local development machine or production machine (server on internet) the same will be happening and you'll always get only the file name and access to byte array which comprises the contents of the file being uploaded. ASP.NET has made this easy with built in APIs and FileUpload control to upload files while it was a bit difficult to perform uploads using earlier technologies.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now