简体   繁体   中英

Upload images from ASP.NET to godaddy file system

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 ;)

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

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.

  1. First of all, you have to change permission of the folder IMAGES [https://www.godaddy.com/help/set-directory-permissions-in-my-windows-hosting-account-16135]. Change these permissions for Application Pool Group (group or user name columns) and give the Full control which will give write and modify permission. Also uncheck the Allow inheritable permissions from the parent to propagate to this object and all child objects. Include these with the entries explicitly defined here .
  2. Also change permission for the folder httpdocs. For Application pool Group for this folder, give add the write permission and as done before, don't forget to uncheck Allow inheritable permissions from the parent to propagate to this object and all child objects. Include these with the entries explicitly defined here .![modification of permission directory of httpdocs][1]: https://i.stack.imgur.com/nAsua.png
  3. Go back to your code to save images (sorry for non csharp developers, it is the language that i am using). Let say that in Images folder, you have another folder which is Administrateurs in which you want to save your image. The directory is now then /httpdocs/IMAGES/Administrateurs. Use the code that you can found in many tutorials, in my case, this code is:
     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).

  1. Use the code (there are many tutorials related to how to save images) to save your image. By doing like that, i have successfully saved my image on godaddy server . Hope that you will also be able to achieve that.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM