I am doing Image uploader in Asp.net and I am giving following code under my controls:
string st;
st = tt.PostedFile.FileName;
Int32 a;
a = st.LastIndexOf("\\");
string fn;
fn = st.Substring(a + 1);
string fp;
fp = Server.MapPath(" ");
fp = fp + "\\";
fp = fp + fn;
tt.PostedFile.SaveAs("fp");
But during uploading or saving image the error message comes that The SaveAs method is configured to require a rooted path, and the path ‘fp’ is not rooted.
So Please help me what is the problem
I suspect the problem is that you’re using the string “fp” instead of the variable
fp. Here’s the fixed code, also made (IMO) more readable:You should also consider changing the penultimate line to:
You might also want to consider what would happen if the posted file used / instead of \ in the filename… such as if it’s being posted from Linux. In fact, you could change the whole of the first three lines to:
Combining these, we’d get:
Much cleaner, IMO 🙂