views:

64

answers:

3

Hello SO:

I am attempting to upload files to my server using ASP.NET MVC. Here is the code that handles the upload request:

foreach (string file in Request.Files)
{
    var hpf = Request.Files[file];
    if (hpf.ContentLength == 0)
    {
        continue;
    }
    var savedFileName = Path.Combine(@"~/uploads", Path.GetFileName(hpf.FileName));
    hpf.SaveAs(Server.MapPath(savedFileName));
}

I keep getting this error:

Access to the path 'C:\HostingSpaces\andersle\anders-leet.com\wwwroot\uploads\{filename}' is denied.

I set the permissions of the upload folder to 777, so from that end it should be OK. Would I have to talk to my hosting company about other permissions (since this is ASP.NET)?

Or is my upload logic completely wrong?

Thanks!

+1  A: 

Should it not be

var savedFileName = Path.Combine(@"~/uploads/", Path.GetFileName(hpf.FileName));

See the extra / after uploads.. or is this something I have missed!

Rippo
I'd expect Path.Combine to do that magic for you...
Rowland Shaw
But apparently (from the error message) it's not doing so.
Dominic Rodger
Oops, I deleted the extra /. Path.Combine does it. Fixed the post.
Anders
lol... OK then. I though Path.Combine should have added the slash... Just being pedantic!!! :)
Rippo
Perms 777 should do the trick... unless they have some weird set-up where the Network service does not apply...Have you tried the app_data folder to upload files to as .net usually does not care about perms for this special folder. However your hosting company might have locked this down as well.
Rippo
The slash was there, I had to do a double slash for it to show up. Possibly because of the curly brace?
Anders
@Rippo - Looks like App_Data is locked down as well. I should probably open a ticket with my hosting provider to see if they can help me further?
Anders
@Anders... Another thought do you have impersonation switched on as your FTP user?
Rippo
A: 

Another thought do you have impersonation switched on as your FTP user?

Rippo
I have no impersonation set up. Is this something I can do (via web.config?)
Anders
yes in system.web<identity impersonate="true" userName="XXX" password="YYY" />
Rippo
Hmm, I am unsure of the user to use. I created a new FTP user, but I get "Could not create Windows user token from the credentials specified in the config file. Error from the operating system 'Logon failure: unknown user name or bad password."
Anders
mmmm... I use Mosso as my hosting company and I have to use:-<identity impersonate="true" userName="dfw\myuser" password="123456789" />However this is not my FTP username as I need to prefix the machine name... Looks like you will need to ask your hosting provider the account the website runs under and use this in your identity impersonation settings. Ho
Rippo
A: 

Turns out I have to change the permissions from my control panel rather than my FTP client. It is now working.

Anders
oh! glad you solved it
Rippo
and thank you for your suggestions@
Anders