Solved

Basic fileupload control in ASP.NET (VB)

Posted on 2014-03-13
4
674 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
  • 2
  • 2
4 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you Very Much
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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 seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

771 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

14 Experts available now in Live!

Get 1:1 Help Now