Solved

Basic fileupload control in ASP.NET (VB)

Posted on 2014-03-13
4
681 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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

820 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