Link to home
Start Free TrialLog in
Avatar of alicia1234
alicia1234Flag for United States of America

asked on

How to specify a rooted path?

I am using <input type="file" id="grpImage" runat="server" /> on a form to let the user upload an image.
In the code-behind, I am trying to save the image on the server to the /uploadedimages directory in my site. (See code below).

The save path is coming out as "c:\uploadedimages\group_1234.jpg" - note the "c:" being tacked on to the front. So I get an error " Could not find a part of the path .. ".

So then I tried setting the image name like this (with the ~):
string imgName = "~/uploadedimages/group_" + strGroupId + "." + fileExt;

But that gives this error:

The SaveAs method is configured to require a rooted path, and the path '~/uploadedimages/group_4294967302.jpg' is not rooted.

What's the correct syntax to use?






if (grpImage.PostedFile.FileName != "")
        {
            string fileNamePath = grpImage.PostedFile.FileName;
            string fileExt = "jpg"; // need logic here to parse the filename to get it

            string imgName = "/uploadedimages/group_" + strGroupId + "." + fileExt;
            grpImage.PostedFile.SaveAs(imgName);
            updatedGroup.GroupImage = "/uploadedimages/skiing_club.jpg";
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alicia1234

ASKER

Perfect! Thank you!