tobbe
tobbe

Reputation: 1807

Get Path to images folder

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?

Upvotes: 2

Views: 4570

Answers (5)

skhurams
skhurams

Reputation: 2145

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

Upvotes: 0

Robbie
Robbie

Reputation: 19500

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

Upvotes: 0

Ghost Answer
Ghost Answer

Reputation: 1498

try this

filuppladdare.PostedFile.SaveAs("~/Project name/.../pictures");

Upvotes: 0

mamoo
mamoo

Reputation: 8166

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)

Upvotes: 2

Guffa
Guffa

Reputation: 700212

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.

Upvotes: 4

Related Questions