Recently I have published my website on a server and all works fine except the upload (is the only section where i need the physical path to my image directory).
If a user wants to upload an image I get the acces denied error.
For the moment all I use to save the file to the server is a FileUpload. This is how I use it:
Upload.aspx
.................
<asp:FileUpload ID="fUpImg" runat="server" />
<asp:DropDownList ID="dropListCategory" runat="server" AutoPostBack="False" DataTextField="Name" DataValueField="Code" Width="227px">
............
Upload.aspx.cs
..................
string imageFolder="Images";
MyImage image= new MyImage() //custom class
image.Name=fUpImg.PostedFile.FileName;
image.categoryCode= dropListCategory.SelectedValue;
imagefolderPath= MapPath(imageFolder + "/" + image.categoryCode);
string finalPath = imageFolderPath +"/"+ fUpImg.PostedFile.FileName;
if (!File.Exists(finalPath))
{
fUpImg.SaveAs(finalPath);
}
I am new to web programming so I really don't know if i can resove this by changing the code or if the server where my website is hoasted dosen't allow this kind of uploads.
I would appreciate any help
Jason ShortPosted Apr 24, 2007, 5:31 AM
You have to write to a directory on the server that you have permissions for WRITE. This is usually the database path on a shared hosting machine. Ask you host what directory your app is allowed to write to, and then use that.
If they tell you something like /databases/ then you can find that directory using:
string realpath = Server.MapPath("~/databases/");
Then you would just write your file to the realpath.
Or if it is your machine you can enable read / write to any path you want, but be careful for security that users can write outside your app.