• A Great friend to the HardForum with a great kid that he is trying to get a scholorship to continue his schooling. Please give hime a vote! Only 24 hours left! Thanks.
    If you have an VOTE FOR KEENAN!

ASP.NET 2.0 FileUpload control headaches

Mabrito

Supreme [H]ardness
Joined
Dec 24, 2004
Messages
7,004
Im designing a website for someone I know. One of the features he wants is a photo album feature where he can create different albums and upload photos to them.

So uploading the photos is the last thing I do need to do for this photo album feature. Im using the FileUpload control in VS2005. Everytime I try to upload a file, it throws an exception and this is what it says when I catch it and show the message: "Access to the path 'C:\Sites\Single50\mabritoj\webroot\Images\Test Album' is denied. " Now that path is off my Brinkster account. But when I debug thru VS, I get the same message. I looked into it and you need to grant the ASP.NET user account on the system write permissions. for the folder your uploading to So I went ahead and granted the account those permissions. I even asked Brinkster to grant me those permissions and they did. Still get this error. Any ideas on how to get this control to work so I can upload photos? Here is my code for my Upload button that will handle the FileUpload control.

Code:
 try
        {
            lblError.Visible = false;
            lblError.Text = "";
            string uploadDir;
            string fullUploadDir;

            if (flUpload1.HasFile == true)
            {
                uploadDir = Server.MapPath("Images/" + ddlCurrentAlbum.SelectedValue);
                fullUploadDir = Server.MapPath("Images/" + ddlCurrentAlbum.SelectedValue + "/" + flUpload1.FileName);

                if (File.Exists(fullUploadDir) == true)
                {
                    lblError.Visible = true;
                    lblError.Text = "This photo album already contains an image with this file name";
                    return;
                }
                else
                {
                    flUpload1.SaveAs(uploadDir);
                }
            }
 
Hmm ... could be a couple of things:
1 - The <space> in the folder name for "Test Album"
2 - The user account that is actually running
3 - Permissions were not really set correctly by the webhost support

The first point is easy enough to see if it solves the problem. Generally speaking, putting spaces in Url's is bad practice.

For the second and a little bit of the third points, you need to find out exactly which account is doing the work. Add a Response.Write statement on your page load to give the output of "Page.User.Identity.Name". If the account that shows up is not what was granted read/write permissions to the folder, then you now know what to tell your webhost.

Also, what OS is the webhost running? IIRC on Server 2003 you also need to grant the IWAM, IUSR, and IIS_WPG read and write access to that folder. Also, the ASPNET account was renamed to NETWORK SERVICE.

HTH !!
 
Hmm ... could be a couple of things:
1 - The <space> in the folder name for "Test Album"
2 - The user account that is actually running
3 - Permissions were not really set correctly by the webhost support

The first point is easy enough to see if it solves the problem. Generally speaking, putting spaces in Url's is bad practice.

For the second and a little bit of the third points, you need to find out exactly which account is doing the work. Add a Response.Write statement on your page load to give the output of "Page.User.Identity.Name". If the account that shows up is not what was granted read/write permissions to the folder, then you now know what to tell your webhost.

Also, what OS is the webhost running? IIRC on Server 2003 you also need to grant the IWAM, IUSR, and IIS_WPG read and write access to that folder. Also, the ASPNET account was renamed to NETWORK SERVICE.

I did add Network Service and gave it all the permissions as well and still a no go.

HTH !!

For your first idea, I did that, still a no go. Good tip though, thanks.

For your second idea, when I run it locally thru VS it shows the account and it does have proper permissions. When I upload the new .CS file on my hosting server and see what account there is running it, it wont show. Must have some restrictions on this.

They are running Server 2003 with IIS 6.
 
Interesting ... try throwing some more Response.Write statements to get the output from:
Code:
System.Threading.Thread.CurrentPrincipal.Identity.Name

System.Security.Principal.WindowsIdentity.GetCurrent().Name

Also, try adding this line to your Web.Config file after the "system.web" tag:
Code:
<system.web>
    <identity impersonate="true"/>
 
Interesting ... try throwing some more Response.Write statements to get the output from:
Code:
System.Threading.Thread.CurrentPrincipal.Identity.Name

System.Security.Principal.WindowsIdentity.GetCurrent().Name

Also, try adding this line to your Web.Config file after the "system.web" tag:
Code:
<system.web>
    <identity impersonate="true"/>

That system.web did something. The photo uploaded but not with the right name or in the right spot.
 
Ok I believe its working now but im just not specifying the write path right and file name.

Am I specifying the path to save it to right? The code in my first post is the code im using.
 
For future reference:

if(FileUpload1.HasFile == true) is unnecessary, not only is this redundant, but the boolean compare makes the code less readable.

if(FileUpload1.HasFile) is not only faster because there is no compare, but it also more closely resembles English, which is the goal of a high level language like C#.

Good luck with file uploads, I hate the file upload control and ended up writing a custom control for a project I was working on at a customer site.

Edit: Also look up the StringBuilder class instead of appending with '+'
 
I know what you are saying but I have 5 of these controls on the page. The admin of this site when I get done will be able to upload 5 images at a time. So if he only has 3 images, then the code will only execute the code for the 3 images.
 
Glad you got it working!

I looked again at your code, and saw that you were calling the SaveAs method with just the path (uploadDir), instead of the complete destination path with the filename (fullUploadDir). Was that change and the Web.Config everything that you had to do?

Anything you can share about your resolution is appreciated.
 
Yep the web config solved the upload problem. I was giving it the wrong directory. Here is the new code I used to give it the right directory and filename:

Code:
 try
        {
            lblError.Visible = false;
            lblError.Text = "";
            string uploadDir;
            string fullUploadDir;
            string WorkingDirectory;
            bool correct;
            int percentage;
            Image imgPhotoVert;
            Image imgPhoto;

            if (flUpload1.HasFile == true)
            {
                uploadDir = Server.MapPath("Images/" + ddlCurrentAlbum.SelectedValue);
                fullUploadDir = Server.MapPath("Images/" + ddlCurrentAlbum.SelectedValue + "/" + flUpload1.FileName);

                if (File.Exists(fullUploadDir) == true)
                {
                    lblError.Visible = true;
                    lblError.Text = "This photo album already contains an image with this file name";
                    return;
                }
                else
                {
                    flUpload1.SaveAs(uploadDir + "\\" + flUpload1.FileName);
                }

            }

I wish MSDN will say you need to do something with the Web.config file to give it proper security permissions. This was driving me nuts all weekend and since the forum was down, I couldnt really ask no one. I tried the official ASP forums by MS, but that didnt help at all. Once again [H] forums always comes thru!!
 
Back
Top