I have an Admin panel on my website, which allows a user to upload images to file system. I'm simply doing in C# code:
imageFile.SaveAs(galleryPath + fileName);
But getting a permissions exception:
Access to the path 'D:\\Hosting...\\html\\Images\\Gallery\\page2-img1.jpg' is denied.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.UnauthorizedAccessException: Access to the path 'D:\\Hosting...\\html\\Images\\Gallery\\page2-img1.jpg' is denied.
Can you please give me a tip how can I grant a permissions?
Read this troubleshooting article, there is a solution there. If that doesn't work just contact the support ;)
Btw, its better practice to use Path.Combine when concatenating folder+file name
imagefile.SaveAs(System.IO.Path.Combine(galleryPath, fileName));
Thanks everyone. I just had to change permissions of folder to Read/Write. By default it's readonly.
Here is more information: http://support.godaddy.com/help/article/6481/setting-directory-permissions-with-windows-hosting-accounts
Ok this is my view
@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>
}
This is my view model
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; }
}
This is my controller Action.
[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.
This is MVC code. If you are using Web Forms then the code should be shorter. I got this from a link but can't find it now so just posted my own code. You also need to make sure that Write permissions are enabled in your host using the Cpanel.
It seems that this problem is regularly faced by developers who host their website to godaddy. Unfortunately, customer service doesn't offer a simple way to resolve the problem. I faced this issue and after many googling as well as try and error, i finally solve the problem. It is true that this problem is related to permission, but not only. The thing is that generally, your website is found in httpdocs folder, meaning that the folder with the name IMAGES which is found in the main root of your website will be /httpdocs/IMAGES.
string cheminImage = "~/httpdocs/IMAGES/Administrateurs"; try { if (!Directory.Exists(HttpContext.Current.Request.MapPath(cheminImage))) { Directory.CreateDirectory(HttpContext.Current.Request.MapPath(cheminImage)); } } catch { }
This only means that if the directory doesn't exist, just create it(i am using .Net 4).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.