asp.net|上傳 異常詳細資料: System.UnauthorizedAccessException: 對路徑“C:\Inetpub\spatial\csk\Communities\Common\Images\HtmlTextBoxUserImages\logo.gif”的訪問被拒絕。
ASP.NET 未被授權訪問所請求的資源。請考慮授予 ASP.NET 請求標識訪問此資源的許可權。ASP.NET 有一個在應用程式沒有類比時使用的基進程標識(通常,在 IIS 5 上為 {MACHINE}\ASPNET,在 IIS 6 上為網路服務)。如果應用程式正在通過 <identity impersonate="true"/> 類比,則標識將為匿名使用者(通常為 IUSR_MACHINENAME)或經過身分識別驗證的請求使用者。
若要授予 ASP.NET 對檔案的寫訪問權,請在資源管理員中右擊該檔案,選擇“屬性”,然後選擇“安全”選項卡。單擊“添加”添加適當的使用者或組。反白 ASP.NET 帳戶,選中所需存取權限對應的框。
源錯誤:
行 23: 行 24: //改變下面的“c:\\"到你想要儲存的地址。 行 25: myFile.PostedFile.SaveAs("C:\\Inetpub\\spatial\\csk\\Communities\\Common\\Images\\HtmlTextBoxUserImages\\"+newnm) ; 行 26: 行 27: //取得上傳檔案的各種屬性。
<%--
upload.aspx
這是一個上傳檔案,從中我們可以看出asp.net的優勢,以前需要控制項才能完成的程式,現在只需要幾行代碼就可以輕鬆完成。
--%>
<%@ Import Namespace="System.IO" %>
<%@ page Language="C#" debug="true" codepage="936"%>
<html>
<head>
<title>檔案上傳,aspcool.com </title>
<script language="C#" runat="server">
public void UploadFile(object sender , EventArgs E)
{
if(myFile.PostedFile!=null)
{
//定義一些變數
string nam = myFile.PostedFile.FileName ;
int i= nam.LastIndexOf("\\") ;
string newnm =nam.Substring(i) ;
//改變下面的“c:\\"到你想要儲存的地址。
myFile.PostedFile.SaveAs("C:\\Inetpub\\spatial\\csk\\Communities\\Common\\Images\\HtmlTextBoxUserImages\\"+newnm) ;
//取得上傳檔案的各種屬性。
fname.Text=myFile.PostedFile.FileName;
fenc.Text=myFile.PostedFile.ContentType ;
fsize.Text=myFile.PostedFile.ContentLength.ToString();
}
}
</script>
</head>
<body>
<center>
<h3> 檔案上傳示範</h3>
<form id="uploderform" method="post" action="upload.aspx" enctype="multipart/form-data" runat="server" >
<table border="1" cellspacing="0" cellpadding="0" >
<tr> <td><h5>選擇檔案</h5></td</tr>
<tr><td>
<input type="file" id="myFile" runat="server" >
</td></tr>
<tr><td>
<input type="button" value="Upload" OnServerClick="UploadFile" runat="server" >
</td></tr>
</table>
</form>
<br>
<br>
<table border="1" cellspacing="0">
<tr><td><b>檔案資料</b></td>
<td> </td>
</tr>
<tr>
<td>檔案名稱 :</td>
<td><asp:label id="fname" text="" runat="server" /></td></tr>
<tr>
<td>檔案類型 :</td>
<td><asp:label id="fenc" runat="server" /></td></tr>
<tr>
<td>檔案大小 :(in bytes)</td>
<td><asp:label id="fsize" runat="server" /></td></tr>
</table>
<br>
<br>
<br>
<h5>你可以從 <a href="http://www.aspcool.com/download">http://www.aspcool.com/download</a> 下載該程式.我們將給您提供更多的其它程式。如果有什麼意見請到我的<a href="http://www.chaxiu.com/club/right.php?boardid=7&page=1">ASP論壇</a>來討論。</h5>
</center>
</body>
</html>