将图像从ASP.NET上传到godaddy文件系统

我的网站上有一个管理员面板,允许用户将图像上传到文件系统。 我只是用C#代码做的:

imageFile.SaveAs(galleryPath + fileName); 

但获得权限exception:

访问路径’D:\ Hosting … \ html \ Images \ Gallery \ page2-img1.jpg’被拒绝。

描述:执行当前Web请求期间发生未处理的exception。 请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。 exception详细信息:System.UnauthorizedAccessException:拒绝访问路径’D:\ Hosting … \ html \ Images \ Gallery \ page2-img1.jpg’。

能否请给我一个提示我如何授予权限?

阅读此故障排除文章,那里有一个解决方案。 如果这不起作用只需联系支持;)

http://support.godaddy.com/help/article/6481/setting-directory-permissions-with-windows-hosting-accounts

顺便说一下,在连接文件夹+文件名时使用Path.Combine的更好做法

 imagefile.SaveAs(System.IO.Path.Combine(galleryPath, fileName)); 

感谢大家。 我只需要将文件夹的权限更改为读/写。 默认情况下,它只是readonly。

以下是更多信息: http : //support.godaddy.com/help/article/6481/setting-directory-permissions-with-windows-hosting-accounts

好的,这是我的观点

  @using (Html.BeginForm("Upload", "Pictures", FormMethod.Post, new { enctype = "multipart/form-data" })) { @Html.AntiForgeryToken() 
Title:
@Html.EditorFor(x => x.Title)
@Html.ValidationMessageFor(x => x.Title)
@Html.TextBoxFor(x => x.File, new { type = "file" })
@Html.ValidationMessageFor(x => x.File)
Description:
@Html.TextAreaFor(x => x.Description)
@Html.ValidationMessageFor(x => x.Description)

}

这是我的视图模型

 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在主机中启用了写入权限。