Maybe I'm doing this wrong, but when I do a Server.MapPath("/images"); it says that it cannot map the path. When I put in Server.MapPath("folder1/images"); it works, but I'm guessing it is building some kind of virtual directory in the same directory location as the original file because if I view the value of the string it will have duplicate folders like "folder1/folder1/images". How do I get around this? I want to use a file upload object logically like this, but how do I do it syntactically?
string filePath = Server.MapPath("\images");
FileUpload1.SaveAs(filepath);
The "images" folder has a virtual path on the server and is where I want all the files uploaded to be put.
Thanks in advance.
You cannot use Server.MapPath to get absolute path of a other virtual directory ( only virtual directory that the application contains in ), my suggestion is you puts the absolute path of the image folder in web.config and then access the value without Server.MapPathThank you for your reply.
I did, however, get the path from the virtual directory. Essentially, this is what one of my other application does (basically looks at the files in the directory and binds them to a DataList for viewing like a gallery):
//Create an array of the images in the physical location of the virtual drive using server.mappath(virtual_drive);string IMAGE_Directory = Server.MapPath("images/large");
ArrayList images =newArrayList();
string html;
string[] filesInDirectory =Directory.GetFiles((IMAGE_Directory),"*.jpg");
int numFilesInDirectory = filesInDirectory.Length;
//loop through the array and write html for each image found in directory and add to the images array
for (int i = 0; i < numFilesInDirectory; i++)
{
html ="<a target='_blank' href='images/Large" + filesInDirectory[i].Substring(filesInDirectory[i].LastIndexOf("\\")) +"'><img border='0' width='100' src='images/Large" + filesInDirectory[i].Substring(filesInDirectory[i].LastIndexOf("\\")) +"'></a><br>Button";
images.Add(html);
}
//bind the array to the datalist
DataList1.DataSource = images;
DataList1.DataBind();
src='" +filesInDirectory[i].ToString() +"'>
This is because it would then look for the file that should be found in the images/Large virtual directory on the machines harddrive (the physical path given from the Server.MapPath). I could view the page on my machine because when it looked, the files were there. However on another machine it was not, so I needed to get the app to look in the virtual directory for the files found in the virtual directory's physical location.
My question was really about whether or not you could do something more like:
string[] filesInDirectory =Directory.GetFiles(Server.MapVirtualPath("images/Large","*.jpg");
And then have an array of images like "/images/Large/image_name".
I got around it in this application and I'll have to continue to fight to get the upload page to save to the physical path of the virtual directory. The problem now is if I do something like:
string dir = Server.MapPath("images"); it will give it the value of something like C:\Inetpub\wwwroot\images, so when I try to save something, it will try to save to that machines C: and not the server's virtual directory (which is the server's C:).
The quest continues...
0 comments:
Post a Comment