2013-10-20 54 views
5

我的網站上有一個管理面板,允許用戶將圖像上傳到文件系統。 我只是做在C#代碼:將圖像從ASP.NET上傳到godaddy文件系統

imageFile.SaveAs(galleryPath + fileName); 

但是,讓權限例外:

訪問路徑「d:\託管... \ HTML \圖片\庫\ page2- 'img1.jpg' 被拒絕。

描述:在執行 當前Web請求期間發生未處理的異常。請查看堆棧跟蹤以獲取有關該錯誤的更多信息以及源代碼的位置。 異常詳細信息:System.UnauthorizedAccessException:訪問 路徑'D:\ Hosting ... \ html \ Images \ Gallery \ page2-img1.jpg'被拒絕。

您能否給我一個提示,我如何授予權限?

+0

如果您在IIS上運行它,看看http://stackoverflow.com/questions/4877741/access-to-the-path-is-denied –

+1

有寫啓用服務器Cpanel上的權限?我在Arvixe託管我的同樣的問題。如果您使用代碼在沒有FTP的情況下上傳,則需要啓用寫入權限。 –

回答

0

確定這是我的看法

@using (Html.BeginForm("Upload", "Pictures", FormMethod.Post, new { enctype = "multipart/form-data" })) 
{ 
    @Html.AntiForgeryToken() 
    <div> 
    Title:<br/> 
    @Html.EditorFor(x => x.Title)<br/> 
    @Html.ValidationMessageFor(x => x.Title)<br/> 
    @Html.TextBoxFor(x => x.File, new { 
    type = "file" 
    })<br/> 
    @Html.ValidationMessageFor(x => x.File)<br/> 
    Description:<br/> 
    @Html.TextAreaFor(x => x.Description)<br/> 
    @Html.ValidationMessageFor(x => x.Description) 
    </div> 
    <div style="clear:both"></div> 
    <p><input type="submit" value="Save"/></p> 
} 

這是我的視圖模型

public class UploadModel 
    { 
     [Required(ErrorMessage=("You have not selected a file"))] 
     public HttpPostedFileBase File { get; set; } 
     [Required(ErrorMessage = "Please enter a title")] 
     [StringLength(50)] 
     public string Title { get; set; } 
     [StringLength(400)] 
     public string Description { get; set; } 
    } 

這是我的控制器動作。

[Authorize(Roles = "Approved")] 
    [HttpPost] 
    [ValidateAntiForgeryToken] 
    public ActionResult Upload(UploadModel m) 
    { 
     byte[] uploadedFile = null; 
     Byte123 xxx = new Byte123(); 
     if (m.File != null && !string.IsNullOrEmpty(m.Title)) 
     { 
      //var fileName = System.IO.Path.GetFileName(m.File.FileName); 
      //string c = m.File.FileName.Substring(m.File.FileName.LastIndexOf(".")); 
      // m.Title = m.Title.Replace(c, ""); 
      uploadedFile = new byte[m.File.InputStream.Length]; //you get the image as byte here but you can also save it to file. 

這是MVC代碼。如果您使用的是Web窗體,那麼代碼應該更短。 我從鏈接中得到了這個,但現在找不到它,所以只是發佈了我自己的代碼。您還需要確保使用Cpanel在您的主機中啓用了寫入權限。

相關問題