C# SaveAs方法配置为需要root路径,路径'~\\images\\594083964.jpg'没有root

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1350977/
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 15:30:53  来源:igfitidea点击:

The SaveAs method is configured to require a rooted path, and the path '~\\images\\594083964.jpg' is not rooted

c#asp.netexception

提问by

The SaveAs method is configured to require a rooted path, and the path '~\images\594083964.jpg' is not rooted. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

SaveAs方法配置为需要root路径,路径'~\images\594083964.jpg'没有root。说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其起源于代码的更多信息。

Exception Details:

异常详情:

System.Web.HttpException: The SaveAs method is configured to require a rooted path, and the path '~\images\594083964.jpg' is not rooted.

System.Web.HttpException: SaveAs 方法被配置为需要有根路径,而路径 '~\images\594083964.jpg' 没有根。

Source Error:

源错误:

Line 27:     {
Line 28: 
Line 29:         fu1.SaveAs(@"~\images\" + i + fu1.FileName.Substring(fu1.FileName.Length - 4, 4));
Line 30: path = "~\images\"+i + fu1.FileName.Substring(fu1.FileName.Length-4,4);
Line 31: }

Source File: e:\PEOPLE\Ravi\new data\WebSite1\signup.aspx.cs Line: 29

源文件:e:\PEOPLE\Ravi\new data\WebSite1\signup.aspx.cs 行:29

回答by MPritchard

The path you are saving to is a relative URL. You need to save to a local file path (or full network path).

您保存到的路径是一个相对 URL。您需要保存到本地文件路径(或完整网络路径)。

Try:

尝试:

string relativePath = @"~\images\"+ i + Path.GetExtension(fu1.FileName);
fu1.SaveAs(Server.MapPath(relativePath));

(Path.GetExtension(string) will handle file extensions that aren't 3 characters too)

(Path.GetExtension(string) 也将处理不是 3 个字符的文件扩展名)