Visual Studio .NET - Error in Uploading file saying uploading is deny
Asked By jinhy82 jinhy82
09-Jun-05 05:25 AM

Hi! I created a empty folder called "Server" in my desktop and try to upload a file to this "Server" folder using ASP.Net, but an error occur:
Error: Access to the path "C:\Documents and Settings\Administrator\Desktop\Server\20000802.zip" is denied.
Any problem in this code?
-------------------------------Page_Load-----------------------------
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim MyServerPath, MyName As String
' Display the names in C:\ that represent directories.
MyServerPath = "C:\Documents and Settings\Administrator\Desktop\Server" ' Set the path.
MyName = Dir(MyServerPath, vbDirectory) ' Retrieve the first entry.
If MyName = "" Then ' The folder is not there & to be created
MkDir("C:\Documents and Settings\Administrator\Desktop\Server\") 'Folder created
End If
End Sub
------------------------Upload button is clicked------------------------
Private Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpload.Click
If Not MyFile.PostedFile Is Nothing And MyFile.PostedFile.ContentLength > 0 Then
' Display properties of the uploaded file
FileName.InnerHtml = MyFile.PostedFile.FileName
FileContent.InnerHtml = MyFile.PostedFile.ContentType
FileSize.InnerHtml = MyFile.PostedFile.ContentLength
UploadDetails.Visible = True
' Let us recover only the file name from its fully qualified path at client
Dim strFileName As String
strFileName = MyFile.PostedFile.FileName
Dim fn As String = System.IO.Path.GetFileName(strFileName) ' only the attched file name not its path
Dim SaveLocation As String = "C:\Documents and Settings\Administrator\Desktop\Server\" & fn
' Let us Save uploaded file to server
Try
MyFile.PostedFile.SaveAs(SaveLocation)
Span1.InnerHtml = "Your File Uploaded Sucessfully at server as : C:\ServerFolder\" & fn
Response.Write("The file has been uploaded.")
Catch Exc As Exception
Response.Write("Error: " & Exc.Message)
Span1.InnerHtml = "An Error occured. Please check the attached file"
UploadDetails.Visible = False
Span2.Visible = False
End Try
Else
Response.Write("Please select a file to upload.")
End If
End Sub
----------------------------------------------------
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="frmUpload.aspx.vb" Inherits="Project.frmUpload"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>frmUpload</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" EncType="multipart/form-data" runat="server">
<Span ID="Span1" Style="COLOR:red" RunAt="Server" /><Span ID="Span2" Style="COLOR:red" RunAt="Server" />
<center>
<TABLE id="Table2" style="Z-INDEX: 101; LEFT: 144px; WIDTH: 416px; POSITION: absolute; TOP: 56px; HEIGHT: 400px"
cellSpacing="1" cellPadding="1" width="416" border="1">
<TR>
<TD style="HEIGHT: 55px"><asp:label id="lblUpload" runat="server" Height="48px" Font-Bold="True" Font-Names="Arial"
font-size="36px" Width="288px">Upload</asp:label></TD>
</TR>
<TR>
<TD></TD>
</TR>
</TABLE>
<TABLE id="Table1" style="Z-INDEX: 102; LEFT: 152px; WIDTH: 400px; POSITION: absolute; TOP: 120px; HEIGHT: 208px"
cellSpacing="1" cellPadding="1" width="400" border="0">
<TBODY>
<TR>
<TD style="WIDTH: 90px; HEIGHT: 19px">Select group :</TD>
<TD style="HEIGHT: 19px"><asp:dropdownlist id="ddlGroup" runat="server" Width="289px"></asp:dropdownlist></TD>
</TR>
<TR>
<TD style="WIDTH: 90px; HEIGHT: 15px">Version :</TD>
<TD style="HEIGHT: 15px"><asp:textbox id="txtPVersion" runat="server" Width="288px"></asp:textbox></TD>
</TR>
<TR>
<TD style="WIDTH: 90px; HEIGHT: 15px">Project Name :</TD>
<TD style="HEIGHT: 15px"><asp:textbox id="txtPName" runat="server" Width="288px"></asp:textbox></TD>
</TR>
<TR>
<TD style="WIDTH: 90px; HEIGHT: 15px">Description :</TD>
<TD style="HEIGHT: 15px"><asp:textbox id="txtPDescription" runat="server" Width="289px" TextMode="MultiLine"></asp:textbox></TD>
</TR>
<TR>
<TD style="WIDTH: 90px; HEIGHT: 32px">File :</TD>
<TD style="HEIGHT: 32px">
<Input ID="MyFile" Type="file" RunAt="Server" Size="28" NAME="MyFile" style="WIDTH: 288px; HEIGHT: 22px"></TD>
</TR>
<TR>
<TD style="WIDTH: 90px; HEIGHT: 39px"></TD>
<TD style="WIDTH: 140px; HEIGHT: 39px">
<asp:button id="btnUpload" runat="server" Width="64px" Text="Upload"></asp:button>
<asp:HyperLink id="hplMenu" runat="server" Width="31px" NavigateUrl="frmMenu.aspx">Menu</asp:HyperLink></TD>
</TR>
<P></P>
<Div ID="UploadDetails" Visible="False" RunAt="Server">
File Name: <Span ID="FileName" RunAt="Server" />
<BR>
File Content: <Span ID="FileContent" RunAt="Server" />
<BR>
File Size: <Span ID="FileSize" RunAt="Server" />bytes
<BR>
</Div>
</TBODY>
</TABLE>
</center>
</form>
</B>
</body>
</HTML>
Asked By Brian Chiasson

You are receiving the error because your application does not have write access to the "Server" directory on the server. If you are NOT using impersonation then you simply need to provide the ASPNET user access to the directory in question. With impersonation enabled, you will need to make sure your impersonated credentials has been granted rights to the directory.
A couple of things in regards to your code...
In your Page_Load event handler, consider wrapping the "code to initialize page" in an if statement so that it only executes the first time the page is loaded.
' Example
If Not (Page.IsPostBack) Then
' Move your code here
End If
I am not sure how long the old VB methods will continue to be supported, so where ever possible I substitute equivalent BCL (base class library) methods and classes to perform the corresponding operations. In particular, the Dir() and MkDir() methods you are using. Take a look at the System.IO.Directory static class for performing these operations.
Check to make sure the file you are uploading is not going to overwrite an existing file. You can do this by using the System.IO.File.Exists(path) method call.
HTH,
Brian
thanks, Brian
Thanks a lot to remind me, Brian. As I am new to ASP.Net, I used to get wrong in writing code. Thanks in reminding me!
Wise for Visual Studio.NET Wise for Visual Studio.NET By Peter A. Bromberg, Ph.D. To "Print This Page" Link Peter Bromberg Wise for Visual Studio .NET is a total and complete installation development system for creating and editing Windows® Installer
Is Visual Studio self-hosting ? .NET Framework Does Microsoft use Visual Studio IDE, Visual Studio Debugger, Visual Studio Linker and Visual Studio compiler for developing Visual Studio ? Or is Visual Studio not
Visual Studio .net .NET Framework Hi NG, ich habe vor längerer Zeit mit Visual Studio .Net 2003 gearbeitet und überlege momentan auf einen neueren Stand upzudaten. Ein Visual Studio .Net 2008 scheint es nicht zu geben. Habe zumindest mit googeln nichts gefunden. Was
Visual Studio versioning . . . . how to tell? .NET Framework To my knowledge, Visual studio 6 was released in 1998, then Visual Studio .NET 2002 is VS 7, then Visual Studio .NET 2003 is VS 7.1, then Visual
visual studio.net 2003 and Access 2007 database .NET Framework Hi I am currently using Visual Studio.Net 2003 running on Windows Server 2000 operating system. I have used Visual Studio.net 2003 connecting to Access 2002 databases in the pass with great success. Now