2

I'm trying to upload pictures to a folder (located in my project folder) using asp:FileUpload.

I'm getting this error when i click the button:

The SaveAs method is configured to require a rooted path, and the path 'localhost:49256/bilder' is not rooted.

Here's my code behind:

protected void ladda_Click(object sender, EventArgs e)
{
    string filename = filuppladdare.FileName;
    string description = desc.Text;

    if (filuppladdare.HasFile)
    {
        filuppladdare.PostedFile.SaveAs(\localhost:21212\pictures");
    }
}

I have only guessed the Path. What should it be? Or how do i get it?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
tobbe
  • 1,737
  • 6
  • 23
  • 40
  • possible duplicate of [The SaveAs method is configured to require a rooted path, and the path '~\\images\\594083964.jpg' is not rooted](http://stackoverflow.com/questions/1350977/the-saveas-method-is-configured-to-require-a-rooted-path-and-the-path-image) – Tim Schmelter Apr 18 '12 at 08:06
  • another possible duplicate: http://stackoverflow.com/questions/1206662/the-saveas-method-is-configured-to-require-a-rooted-path-and-the-path-fp-is-n – scheien Apr 18 '12 at 08:08

5 Answers5

4

Use the MapPath method to get the physical path of a folder:

filuppladdare.PostedFile.SaveAs(Server.MapPath("~/pictures"));

The ~ in the path represents the application root.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Server.MapPath documentation: http://msdn.microsoft.com/en-us/library/ms524632%28v=vs.90%29.aspx – Artem Koshelev Apr 18 '12 at 08:08
  • seems to work, but now I'm getting "Access to the path 'thecorrectpathon my disk...\visual studio 2010\Projects\labb6\labb6\bilder' is denied." instead, when trying to upload. So the path seems to be right – tobbe Apr 18 '12 at 08:17
  • @guffa see above :) (do i have to write @ your username for you to see my question?) – tobbe Apr 18 '12 at 08:24
  • @tobbe: Yes, you need to give write permission to the folder for the user account running the code. I get notified when you comment my answer without the @, but this is a forum, not a chat, so it may take a while before you get an answer. :) – Guffa Apr 18 '12 at 08:26
  • oh, hehe sorry. Didn't know how it works. But now I do, thanks! – tobbe Apr 18 '12 at 08:32
2

The path to use refers to a physical path on your server (or in another accessible FS path), not to a url of your website.

If you want to get the physical path from the url you should use Server.MapPath(yourUrl)

mamoo
  • 8,156
  • 2
  • 28
  • 38
  • seems to work, but now I'm getting "Access to the path 'thecorrectpathon my disk...\visual studio 2010\Projects\labb6\labb6\bilder' is denied." instead, when trying to upload. Any idea how to fix that? – tobbe Apr 18 '12 at 08:20
  • Check your folder security properties, and also check that the website is running in an Application Pool that can properly access the FS, and that the user running the IIS process has access too... see here: http://stackoverflow.com/questions/5437723/iis-apppoolidentity-and-file-system-write-access-permissions – mamoo Apr 18 '12 at 08:22
0

try this

filuppladdare.PostedFile.SaveAs("~/Project name/.../pictures");
Ghost Answer
  • 1,458
  • 1
  • 17
  • 41
0

There are various ways to get the current path, this link shows you how to use various properties from HttpContext to find paths:

in your example, you could use the following:

filuppladdare.PostedFile.SaveAs("~\pictures");

The ~ tells it to start from the root of your application.

You could also use Server.MapPath

Robbie
  • 18,750
  • 4
  • 41
  • 45
0

one option is to use Server MapPath and sencond check folder permissions may be that can help

skhurams
  • 2,133
  • 7
  • 45
  • 82