11

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

Majid
  • 12,271
  • 14
  • 71
  • 107

6 Answers6

28

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:

string filename = tt.PostedFile.FileName;
int lastSlash = filename.LastIndexOf("\\");
string trailingPath = filename.Substring(lastSlash + 1);
string fullPath = Server.MapPath(" ") + "\\" + trailingPath;
tt.PostedFile.SaveAs(fullPath);

You should also consider changing the penultimate line to:

string fullPath = Path.Combine(Server.MapPath(" "), trailingPath);

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:

string trailingPath = Path.GetFileName(tt.PostedFile.FileName));

Combining these, we'd get:

string trailingPath = Path.GetFileName(tt.PostedFile.FileName));
string fullPath = Path.Combine(Server.MapPath(" "), trailingPath);
tt.PostedFile.SaveAs(fullPath);

Much cleaner, IMO :)

Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • Thanks sir now no error is there but when I debug images are not shown in Gridview. –  Jul 31 '09 at 11:45
  • Without knowing what you're doing, it's hard to help you. I suggest you ask another question with more information. – Jon Skeet Jul 31 '09 at 12:29
  • Sir I had gridview in design view and in that I have 2 coloum Name,class,image. So for image I have fileuploader in Edititemtemplate as well in fottertemplate. But sir when I upload any image there is no error but images are not shown, same problem while updating. I am giving in source of image (itemtemplate) this code - –  Aug 01 '09 at 06:27
  • 1
    This isn't suitable for dealing with in comments. Ask a new question. – Jon Skeet Aug 01 '09 at 06:31
  • In short, I had kind of the same problem as @Narinder described, got an error saying something like cannot / not allowed to access this file. That is because for the website, you still need to use the relative path. `SO: use the full path to upload, use or make a relative path and store that in your database or whatever.` – CularBytes Jan 22 '15 at 22:50
  • Check this too.. http://stackoverflow.com/questions/5039725/server-mappath-physical-path-given-virtual-path-expected – Bino Kochumol Varghese Jul 22 '16 at 08:27
  • amazingly enough, i had the same exact problem (quoting my variable) – Yoav24 Jul 20 '17 at 18:25
6

Use Server.MapPath():

fileUploader.SaveAs(Server.MapPath("~/Images/")+"file.jpg");
Majid
  • 12,271
  • 14
  • 71
  • 107
2

If you want to save the uploaded file to the value of fp, just pass it in, don't put it in quotes:

tt.PostedFile.SaveAs(fp);
cjk
  • 43,338
  • 9
  • 74
  • 109
2

When reading the title of the question, I was thinking that it looked like you had put quotation marks around the variable name. Not really believing that it was so, I opened the question to read it, but it really was so...

Guffa
  • 640,220
  • 96
  • 678
  • 956
1

I encountered the same problem. The problem is that you did not specify the path of the server that you want the file to be saved. And here is a probably simpler answer:

string fileName = tt.PostedFile.FileName;
string savePath = Server.MapPath("Path/Of/The/Folder/Comes/Here/") + fileName);
tt.PostedFile.SaveAs(savePath);
johnnyRose
  • 6,243
  • 16
  • 40
  • 58
1

We cannot use the "SaveAs" method to write directly to an FTP server. Only local paths and UNC paths are supported for the above method.

To save it to FTP, please use the FtpWebRequest class.

You will get the full details to this in the same type of question answer in social.msdn.

Please go through the link.. and you will be able to solve the issue..

enter link description here

--thanks for the answer by Jesse HouwingXPirit (MCC, Partner, MVP)