?
Solved

Get file path on client's machine

Posted on 2009-05-09
5
Medium Priority
?
1,725 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Technology Partners: 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!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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!
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

777 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