Solved

Basic fileupload control in ASP.NET (VB)

Posted on 2014-03-13
4
692 Views
Last Modified: 2014-03-13
Experts

im trying to test a simple fileupload control in an ASP.NET page

i put in a fileupload control, a button, and a status message but it wont upload the file

here is my code

Protected Sub uploadbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uploadbutton.Click
        If FileUpload1.HasFile Then
            Try
                Dim filename As String = Path.GetFileName(FileUpload1.FileName)
                FileUpload1.SaveAs(HttpContext.Current.Server.MapPath("C:\FileDestination") & filename)
                statuslabel.Text = "Upload status: File uploaded!"
            Catch ex As Exception
                statuslabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message
            End Try
        End If
    End Sub
End Class

Open in new window


and the error I get is

'C:/FileDestination' is a physical path, but a virtual path was expected.

what did i do wrong ?
0
Comment
Question by:plusone3055
[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
  • 2
  • 2
4 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
ID: 39926968
Your call to HttpContext.Current.Server.MapPath is the problem in that you are specifying a physical path where MapPath is used to return the physical path of a virtual path, so essentially you don't need this in your code.

you just need : FileUpload1.SaveAs("C:\FileDestination" & filename)

Protected Sub uploadbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uploadbutton.Click
        If FileUpload1.HasFile Then
            Try
                Dim filename As String = Path.GetFileName(FileUpload1.FileName)
                FileUpload1.SaveAs("C:\FileDestination" & filename)
                statuslabel.Text = "Upload status: File uploaded!"
            Catch ex As Exception
                statuslabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message
            End Try
        End If
    End Sub
End Class 

Open in new window

0
 
LVL 22

Author Comment

by:plusone3055
ID: 39927026
interesting it said the file uploaded but when I look in the folder its not there ????
0
 
LVL 29

Accepted Solution

by:
Paul Jackson earned 500 total points
ID: 39927291
For a call to the SaveAs to work, the ASP.NET application must have write access to the directory on the server. There are two ways that the application can get write access. You can explicitly grant write access to the account under which the application is running, in the directory in which the uploaded files will be saved. Alternatively, you can increase the level of trust that is granted to the ASP.NET application. To get write access to the executing directory for the application, the application must be granted the AspNetHostingPermission object with the trust level set to the AspNetHostingPermissionLevel.Medium value. Increasing the level of trust increases the application's access to resources on the server. Note that this is not a secure approach, because a malicious user who gains control of your application will also be able to run under this higher level of trust. It is a best practice to run an ASP.NET application in the context of a user who has the minimum privileges that are required for the application to run.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas(v=vs.110).aspx
0
 
LVL 22

Author Closing Comment

by:plusone3055
ID: 39927384
Thank you Very Much
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

696 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