The SaveAs method is configured to require a roote

2020-02-01 06:56发布

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

标签: c# .net file
6条回答
冷血范
2楼-- · 2020-02-01 07:36

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...

查看更多
啃猪蹄的小仙女
3楼-- · 2020-02-01 07:36

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);
查看更多
Viruses.
4楼-- · 2020-02-01 07:37

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)

查看更多
家丑人穷心不美
5楼-- · 2020-02-01 07:38

Use Server.MapPath():

fileUploader.SaveAs(Server.MapPath("~/Images/")+"file.jpg");
查看更多
【Aperson】
6楼-- · 2020-02-01 07:53

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 :)

查看更多
趁早两清
7楼-- · 2020-02-01 07:53

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);
查看更多
登录 后发表回答