我写了一些ASP.NET代码,以便在新客户注册时在远程服务器上创建一个新文件夹。
这个想法是所有新客户上传的文件都将驻留在他/她的文件夹中......
当我在本地开发机器上运行注册过程时,一切正常,一旦上传文件并设置了远程服务器文件夹权限,它就会轰炸....
我的托管服务提供商提供了以下示例代码和信息......
托管/脚本编写&编程/ ASP.NET 使用ASP.NET创建新目录
Sample Code....
<%@ Import Namespace="System.IO"%>
<html>
<head>
<title>File Download</title>
<script language="VB" runat="server">
Sub createFolder(sender As Object, e As EventArgs)
If ((dirname.Text) = "") Then
message1.Text = "Error: you must enter directory name"
Else
Directory.CreateDirectory(Server.MapPath("./" + dirname.Text))
message1.Text = "Directory successfully created"
End If
End Sub
</script>
</head>
<body>
<form runat="server">
DirName:
<asp:Textbox id=dirname size=14 runat=server /><br/>
<asp:Button Text="Create Directory" OnClick="createFolder" Runat="Server"/><br/>
<asp:Label id="message1" Runat="Server"/><br/>
<asp:Label id="Folders" EnableViewState="False" Runat="Server"/><br/>
</form>
</body>
</html>
其他信息: 您必须使用服务器应用程序更改文件/文件夹权限,以授予保存脚本的文件夹的写入权限。请确保IUSR和NETWORK都具有写入权限。您还必须确保Web.Config文件中存在以下行,以使此脚本起作用:
<identity impersonate="true"/>
请注意: ASP.Net的“CreateDirectory”方法需要特殊权利。此权限仅适用于IUSR帐户。为了确保您的请求由IUSR处理,您必须将上述行放在Web.Config中。
这是我的注册页面中的代码
'Submitted
If Trim(Request("btnSubmit")) = "Submit" Then
'validate-account
_pgTest = Me.Master.MstrHsaMan.IsValidUserRecord("New", 0, 1, Request("txtUserName"), Request("txtEmail"), Request("txtPassword"), Request("txtVerify"), clsDrz_HSA_Declarations.UserStatusCode.UserPending, Me.Master.MstrError)
If _pgTest Then
'validate-company
_pgTest = Me.Master.MstrHsaMan.IsValidCompanyRecord("New", 0, 0, Request("txtCompany"), Request("txtContactee"), Request("txtPhone"), Request("txtFax"), Request("txtMobile"), Request("txtCEmail"), Request("txtWebUrl"), clsDrz_HSA_Declarations.UserStatusCode.UserPending, Me.Master.MstrError)
If _pgTest Then
'save record
_pgTest = Me.Master.MstrHsaMan.SaveNewCompanyRecord(Request("txtCompany"), Request("txtContactee"), Request("txtPhone"), Request("txtFax"), Request("txtMobile"), Request("txtCEmail"), Request("txtWebUrl"), _pgCompany, Me.Master.MstrError)
If _pgTest Then
'generate new company folder
_pgTest = Me.Master.MstrHsaMan.SpawnCompanyFolder(Server.MapPath("System"), _pgCompany.CompanyId, Me.Master.MstrError)
If _pgTest Then
'generate new slides folder
_pgTest = Me.Master.MstrHsaMan.SpawnSlidesFolder(Server.MapPath("System/Company" & _pgCompany.CompanyId), Me.Master.MstrError)
If _pgTest Then
'generate new videos folder
_pgTest = Me.Master.MstrHsaMan.SpawnVideosFolder(Server.MapPath("System/Company" & _pgCompany.CompanyId), Me.Master.MstrError)
If _pgTest Then
'save user account record
_pgTest = Me.Master.MstrHsaMan.SaveNewUserRecord(_pgCompany.CompanyId, Request("txtUserName"), Request("txtEmail"), Request("txtPassword"), _pgUser, Me.Master.MstrError)
If _pgTest Then
'SUCCESS saved
Me.Master.MstrHsaMan.UpdateSuccessObjectParameters("Save New Company", "Company", Request("txtCompany"), "Contactee", Request("txtContactee"), "Telephone", Request("txtPhone"), "Fax", Request("txtFax"), "Mobile", Request("txtMobile"), Me.Master.MstrSuccess)
HSAds_SuccessCtrl1.CtrlSuccessInfo = Me.Master.MstrSuccess
Else
'error detected
Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Save New Account", "SaveNewUserRecord", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
End If
Else
'error detected
Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Save New Company", "SpawnVideosFolder", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
End If
Else
'error detected
Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Save New Company", "SpawnSlidesFolder", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
End If
Else
'error detected
Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Save New Company", "SpawnCompanyFolder", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
End If
Else
'error detected
Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Save New Company", "SaveNewCompanyRecord", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
End If
Else
'error detected
Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Validate New Company", "IsValidCompanyRecord", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
End If
Else
'error detected
Me.Master.MstrHsaMan.UpdateErrorObjectParameters("Validate New Account", "IsValidAccountRecord", "Register.Aspx", Me.Master.MstrError.ErrExternal, False, Me.Master.MstrError)
HSAds_ErrorCtrl1.CtrlErrorInfo = Me.Master.MstrError
End If
End If
'
这些是我用来控制远程服务器上新文件夹生成的例程......
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
'-> [FUNCTION] SpawnCompanyFolder
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Function SpawnCompanyFolder(ByVal pSvrRootPath As String, ByVal pCompanyId As Long, ByRef pRtnErrObj As clsDrz_HSA_Error) As Boolean
'
Dim sxFolder As String = Nothing
'
'-> Initialise
SpawnCompanyFolder = False
pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqUNINITIALISED
pRtnErrObj.ErrTitle = ""
pRtnErrObj.ErrDescription = ""
pRtnErrObj.ErrExternal = ""
'-> Process Request
If Trim(pSvrRootPath) <> "" Then
If pCompanyId > 0 Then
sxFolder = "Company" & pCompanyId
If fnCreateFolder(sxFolder, pSvrRootPath, pRtnErrObj) Then
SpawnCompanyFolder = True
Else
'Create company folder error
Select Case pRtnErrObj.ErrId
Case clsDrz_HSA_Errors.eSYS_FAILEDTOCREATEFOLDER
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eCMP_FAILEDTOCREATEFOLDER
pRtnErrObj.ErrDescription = "The company folder (" & sxFolder & ") could not be created."
Case clsDrz_HSA_Errors.eSYS_FOLDERALREADYEXISTS
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eCMP_FOLDERALREADYEXISTS
pRtnErrObj.ErrDescription = "The company folder (" & sxFolder & ") could not be created."
Case clsDrz_HSA_Errors.eSYS_FOLDERNAMEMISSING
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eCMP_FOLDERNAMEMISSING
pRtnErrObj.ErrDescription = "The company folder name was found to be missing."
Case Else
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_UNKNOWNDIRECTORYERROR
pRtnErrObj.ErrTitle = "Unforseen File System Error!"
pRtnErrObj.ErrDescription = "An unforseen file system access error has occurred."
pRtnErrObj.ErrExternal = "It is recommended to restart your browsing session."
End Select
End If
Else
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eCMP_COMPANYFOLDERIDINVALID
pRtnErrObj.ErrTitle = "Invalid Folder Company Id!"
pRtnErrObj.ErrDescription = "An invalid folder company-id parameter was passed into a function call."
pRtnErrObj.ErrExternal = ""
End If
Else
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eCMP_SERVERPATHMISSING
pRtnErrObj.ErrTitle = "Server Path Not Specified!"
pRtnErrObj.ErrDescription = "The server mapped path was not specified or was found to be missing."
pRtnErrObj.ErrExternal = ""
End If
sxFolder = Nothing
'
End Function
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
'-> [FUNCTION] SpawnSlidesFolder
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Function SpawnSlidesFolder(ByVal pSvrRootPath As String, ByRef pRtnErrObj As clsDrz_HSA_Error) As Boolean
'
Dim sxFolder As String = Nothing
'
'-> Initialise
SpawnSlidesFolder = False
pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqUNINITIALISED
pRtnErrObj.ErrTitle = ""
pRtnErrObj.ErrDescription = ""
pRtnErrObj.ErrExternal = ""
'-> Process Request
If Trim(pSvrRootPath) <> "" Then
sxFolder = "Slides"
If fnCreateFolder(sxFolder, pSvrRootPath, pRtnErrObj) Then
SpawnSlidesFolder = True
Else
'Create company folder error
Select Case pRtnErrObj.ErrId
Case clsDrz_HSA_Errors.eSYS_FAILEDTOCREATEFOLDER
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSLD_FAILEDTOCREATEFOLDER
pRtnErrObj.ErrDescription = "The company slides folder (" & sxFolder & ") could not be created."
Case clsDrz_HSA_Errors.eSYS_FOLDERALREADYEXISTS
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSLD_FOLDERALREADYEXISTS
pRtnErrObj.ErrDescription = "The company slides folder (" & sxFolder & ") could not be created."
Case clsDrz_HSA_Errors.eSYS_FOLDERNAMEMISSING
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSLD_FOLDERNAMEMISSING
pRtnErrObj.ErrDescription = "The company slides folder name was found to be missing."
Case Else
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_UNKNOWNDIRECTORYERROR
pRtnErrObj.ErrTitle = "Unforseen File System Error!"
pRtnErrObj.ErrDescription = "An unforseen file system access error has occurred."
pRtnErrObj.ErrExternal = "It is recommended to restart your browsing session."
End Select
End If
Else
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSLD_SERVERPATHMISSING
pRtnErrObj.ErrTitle = "Server Path Not Specified!"
pRtnErrObj.ErrDescription = "The server mapped path was not specified or was found to be missing."
pRtnErrObj.ErrExternal = ""
End If
sxFolder = Nothing
'
End Function
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
'-> [FUNCTION] SpawnVideosFolder
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Function SpawnVideosFolder(ByVal pSvrRootPath As String, ByRef pRtnErrObj As clsDrz_HSA_Error) As Boolean
'
Dim sxFolder As String = Nothing
'
'-> Initialise
SpawnVideosFolder = False
pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqUNINITIALISED
pRtnErrObj.ErrTitle = ""
pRtnErrObj.ErrDescription = ""
pRtnErrObj.ErrExternal = ""
'-> Process Request
If Trim(pSvrRootPath) <> "" Then
sxFolder = "Videos"
If fnCreateFolder(sxFolder, pSvrRootPath, pRtnErrObj) Then
SpawnVideosFolder = True
Else
'Create company folder error
Select Case pRtnErrObj.ErrId
Case clsDrz_HSA_Errors.eSYS_FAILEDTOCREATEFOLDER
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eVDO_FAILEDTOCREATEFOLDER
pRtnErrObj.ErrDescription = "The company videos folder (" & sxFolder & ") could not be created."
Case clsDrz_HSA_Errors.eSYS_FOLDERALREADYEXISTS
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eVDO_FOLDERALREADYEXISTS
pRtnErrObj.ErrDescription = "The company videos folder (" & sxFolder & ") could not be created."
Case clsDrz_HSA_Errors.eSYS_FOLDERNAMEMISSING
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eVDO_FOLDERNAMEMISSING
pRtnErrObj.ErrDescription = "The company videos folder name was found to be missing."
Case Else
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_UNKNOWNDIRECTORYERROR
pRtnErrObj.ErrTitle = "Unforseen File System Error!"
pRtnErrObj.ErrDescription = "An unforseen file system access error has occurred."
pRtnErrObj.ErrExternal = "It is recommended to restart your browsing session."
End Select
End If
Else
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eVDO_SERVERPATHMISSING
pRtnErrObj.ErrTitle = "Server Path Not Specified!"
pRtnErrObj.ErrDescription = "The server mapped path was not specified or was found to be missing."
pRtnErrObj.ErrExternal = ""
End If
sxFolder = Nothing
'
End Function
创建文件夹的实际功能如下......
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
'-> [FUNCTION] fnFolderExists
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Function fnFolderExists(ByVal pFldrPath As String, ByRef pRtnErrObj As clsDrz_HSA_Error) As Boolean
'
Dim sxTest As Boolean = Nothing
Dim sxDirectory As System.IO.DirectoryInfo = Nothing
'
fnFolderExists = False
pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqUNINITIALISED
pRtnErrObj.ErrTitle = ""
pRtnErrObj.ErrDescription = ""
pRtnErrObj.ErrExternal = ""
If Trim(pFldrPath) <> "" Then
Try
sxDirectory = New System.IO.DirectoryInfo(pFldrPath)
sxTest = sxDirectory.Exists()
If sxTest Then
pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqNOERRORSFOUND
pRtnErrObj.ErrTitle = ""
pRtnErrObj.ErrDescription = ""
pRtnErrObj.ErrExternal = ""
fnFolderExists = True
Else
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_FOLDERDOESNOTEXIST
pRtnErrObj.ErrTitle = "Specified Folder Not Found!"
pRtnErrObj.ErrDescription = "The folder specified (" & pFldrPath & ") could not be found at the path specified."
pRtnErrObj.ErrExternal = ""
End If
Catch ex As Exception
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_FOLDERDOESNOTEXIST
pRtnErrObj.ErrTitle = "Specified Folder Not Found!"
pRtnErrObj.ErrDescription = "The folder specified (" & pFldrPath & ") could not be found at the path specified."
pRtnErrObj.ErrExternal = ""
End Try
Else
fnFolderExists = clsDrz_HSA_Errors.eSYS_FOLDERNAMEMISSING
pRtnErrObj.ErrTitle = "Folder Name Missing!"
pRtnErrObj.ErrDescription = "No folder name/path specified."
pRtnErrObj.ErrExternal = ""
End If
sxTest = Nothing
sxDirectory = Nothing
'
End Function
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
'-> [FUNCTION] fnCreateFolder
'--------------------------------------------------------------------------------------------------------------------------------------------------------------
Private Function fnCreateFolder(ByVal pNewFolder As String, ByVal pInFolder As String, ByRef pRtnErrObj As clsDrz_HSA_Error) As Boolean
'
fnCreateFolder = False
pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqUNINITIALISED
pRtnErrObj.ErrTitle = ""
pRtnErrObj.ErrDescription = ""
pRtnErrObj.ErrExternal = ""
If Trim(pInFolder) <> "" Then
If Trim(pNewFolder) <> "" Then
Try
' Determine whether the SOURCE directory exists.
If fnFolderExists(pInFolder, pRtnErrObj) Then
If fnFolderExists(pNewFolder, pRtnErrObj) Then
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_NEWFOLDERALREADYEXISTS
pRtnErrObj.ErrTitle = "Folder Exists Already!"
pRtnErrObj.ErrDescription = "The new folder could not be created."
pRtnErrObj.ErrExternal = "The folder (" & pNewFolder & ") could not be created because it exists already."
Else
Try
System.IO.Directory.SetCurrentDirectory(pInFolder)
Dim sxNewDir As System.IO.DirectoryInfo = System.IO.Directory.CreateDirectory(pNewFolder)
pRtnErrObj.ErrId = clsDrz_HSA_Declarations.DrZedActionState.drzHqNOERRORSFOUND
pRtnErrObj.ErrTitle = ""
pRtnErrObj.ErrDescription = ""
pRtnErrObj.ErrExternal = ""
fnCreateFolder = True
Catch ex As Exception
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_FAILEDTOCREATEFOLDER
pRtnErrObj.ErrTitle = "Failed To Create Folder!"
pRtnErrObj.ErrDescription = "The folder (" & pNewFolder & ") could not be created."
pRtnErrObj.ErrExternal = ex.Message
End Try
End If
Else
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_INFOLDERMISSING
pRtnErrObj.ErrTitle = "Source Folder Missing!"
pRtnErrObj.ErrDescription = "The source folder was found to be missing."
pRtnErrObj.ErrExternal = "The new folder could not be created because the source folder does not exist."
End If
Catch ex As Exception
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_FAILEDTOCREATEFOLDER
pRtnErrObj.ErrTitle = "Failed To Create Folder!"
pRtnErrObj.ErrDescription = "The folder (" & pNewFolder & ") could not be created."
pRtnErrObj.ErrExternal = ex.Message
End Try
Else
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_NEWFOLDERNAMEMISSING
pRtnErrObj.ErrTitle = "New Folder Name Missing!"
pRtnErrObj.ErrDescription = "The new folder name was found to be missing."
pRtnErrObj.ErrExternal = "The new folder could not be created because no name was specified."
End If
Else
pRtnErrObj.ErrId = clsDrz_HSA_Errors.eSYS_INFOLDERNAMEMISSING
pRtnErrObj.ErrTitle = "Source Folder Name Missing!"
pRtnErrObj.ErrDescription = "The source folder name was found to be missing."
pRtnErrObj.ErrExternal = "The new folder could not be created because no source folder name was specified."
End If
'
End Function
据我所知,所有代码都是正确且无错误的,并且可以在我的开发服务器上无缝运行....
我觉得这可能是web.config文件中的配置问题,我现在卡住了,并没有从支持中获得太多帮助,支持部门建议我检查我的代码!
请有人查看我上面的代码和下面的配置文件,并指出我正确的方向......
非常感谢
平台是.NET 4.0 / 4.5 托管是共享Windows,运行在中等信任。
我不确定我的应用程序是否正在尝试完全信任(?)
我的数据文件夹权限允许(修改/读取和执行/读/写) 我的数据文件夹权限拒绝(没有被拒绝)
我的数据文件夹父文件夹权限允许(修改/读取和执行/读/写) 我的数据文件夹父文件夹权限拒绝(没有拒绝)
我的web.config文件
<!-- Web.Config Configuration File -->
<configuration>
<system.web>
<customErrors mode="Off"/>
<!-- identity impersonate="true"/ -->
<!--
Line above commented out as it causes the site to crash
without loading any pages and the error points to this line
-->
</system.web>
</configuration>
错误讯息....