Solved

Get file path on client's machine

Posted on 2009-05-09
5
1,581 Views
Last Modified: 2013-12-17
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
Comment
Question by:KaranGupta
  • 3
5 Comments
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24346943
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
 
LVL 9

Expert Comment

by:tculler
ID: 24347071
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
 
LVL 11

Expert Comment

by:Muhammad Ousama Ghazali
ID: 24347530
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
 

Author Comment

by:KaranGupta
ID: 24348527
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
 
LVL 11

Accepted Solution

by:
Muhammad Ousama Ghazali earned 500 total points
ID: 24348598
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now