Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why this error is comming - 'Path' is not declared. It may be inaccessible due to its protection level.

My code is given below

ASP.NET
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="uploadfile.aspx.vb" Inherits="uploadfile" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
       <p>
            Select File1:<br />
            <input id="File1" type="file"  runat="server" />
            <br />
            Select File2:<br />
            <input id="File2" type="file"  runat="server" />
            <br />
            Select File3:<br />
            <input id="File3" type="file"  runat="server" />
            <br />
            Select File4:<br />
            <input id="File4" type="file"  runat="server" />
        </p>
        <p>
            <asp:Button ID="Button1" runat="server" Text="Upload File" />
 <br />
        </p>
        <span id="Span1"  runat="server">

    </div>
    </form>
</body>
</html>


VB
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim filepath As String = "C:\Uploads"
        Dim uploadedFiles As HttpFileCollection = Request.Files
        Dim i As Integer = 0

        Do Until i = uploadedFiles.Count
            Dim userPostedFile As HttpPostedFile = uploadedFiles(i)

            Try
                If (userPostedFile.ContentLength > 0) Then
                    Span1.InnerHtml += "File #" & (i + 1) & "<br>"
                    Span1.InnerHtml += "File Content Type: " & userPostedFile.ContentType & "<br>"
                    Span1.InnerHtml += "File Size: " & userPostedFile.ContentLength & "kb<br>"
                    Span1.InnerHtml += "File Name: " & userPostedFile.FileName & "<br>"

                    userPostedFile.SaveAs(filepath & "C:\inetpub\wwwroot" & Path.GetFileName(userPostedFile.FileName))

                    Span1.InnerHtml += "Location where saved: " & filepath & "\" & Path.GetFileName(userPostedFile.FileName) & "<p>"
                End If
            Catch ex As Exception
                Span1.InnerHtml += "Error:<br>" & ex.Message
            End Try
            i += 1
        Loop

    End Sub
</br></p></br></br></br></br>
Posted
Updated 15-Nov-11 22:59pm
v2
Comments
[no name] 16-Nov-11 4:59am    
Format your code snippets like everyone else here does!!

You have
VB
filepath = "C:\Uploads"

and the name of the file you are trying to save as
VB
filepath & "C:\inetpub\wwwroot" & Path.GetFileName(userPostedFile.FileName)

which means that you are trying to save a file with a name like
VB
"C:\UploadsC:\inetpub\wwwrootFileName.Ext"

I'm not surprised you are getting an error message.
 
Share this answer
 
You mean you have compilation error on line
C#
userPostedFile.SaveAs(filepath & "C:\inetpub\wwwroot" & Path.GetFileName(userPostedFile.FileName))

?
Make sure you added
C#
using System.IO;

at the top of the file
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900