Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Basic fileupload control in ASP.NET (VB)

Posted on 2014-03-13
4
Medium Priority
?
706 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

650 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