Solved

Get file path on client's machine

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

776 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