2013-10-20 58 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在您的主机中启用了写入权限。

相关问题