C# SaveAs方法配置为需要root路径,路径'fp'没有root

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1206662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:55:12  来源:igfitidea点击:

The SaveAs method is configured to require a rooted path, and the path 'fp' is not rooted

c#.netfile

提问by

I am doing Image uploader in Asp.net and I am giving following code under my controls:

我正在 Asp.net 中执行图像上传程序,并在我的控制下提供以下代码:

    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

但是在上传或保存图像时,会出现错误消息,即SaveAs 方法配置为需要有根路径,而路径 'fp' 没有根。所以请帮助我有什么问题

采纳答案by Jon Skeet

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:

我怀疑问题在于您使用的是字符串 "fp" 而不是变量fp。这是固定代码,也使(IMO)更具可读性:

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:

您可能还想考虑如果发布的文件在文件名中使用 / 而不是 \ 会发生什么……例如,如果它是从 Linux 发布的。实际上,您可以将前三行全部更改为:

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

更干净,IMO :)

回答by cjk

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

如果要将上传的文件保存为fp的,直接传入即可,不要加引号:

tt.PostedFile.SaveAs(fp);

回答by Guffa

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

在阅读问题的标题时,我认为您似乎在变量名称周围加上了引号。不是真的相信是这样,我打开问题阅读它,但它真的是这样......

回答by Olanrewaju O. Joseph

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

回答by Jineesh Uvantavida

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.

我们不能使用“另存为”方法直接写入 FTP 服务器。上述方法仅支持本地路径和 UNC 路径。

To save it to FTP, please use the FtpWebRequestclass.

要将其保存到 FTP,请使用FtpWebRequest类。

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

您将在 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)

--感谢Jesse HouwingXPirit(MCC、合作伙伴、MVP)的回答

回答by Majid

Use Server.MapPath():

使用Server.MapPath()

fileUploader.SaveAs(Server.MapPath("~/Images/")+"file.jpg");