Solved

Get file path on client's machine

Posted on 2009-05-09
5
1,590 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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