C# 如何将文件保存在asp.net中的文件夹中-路径名错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17935681/
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
how to save files in a folder in asp.net- error in path name
提问by user2584832
protected void Button1_Click(object sender, EventArgs e)
{
FileUpload1.SaveAs(Server.MapPath("blablabla//" + FileUpload1.FileName));
}
This is the code behind my button for uploading a file to a folder in my web application in ASP.NET
这是用于将文件上传到 ASP.NET 中 Web 应用程序中的文件夹的按钮背后的代码
But im getting an error that says DirectoryNotFoundException was unhandled by user code
.
但我收到一个错误,说DirectoryNotFoundException was unhandled by user code
.
How can i solve this error? The problem is the path name that leads to my folder called "blablabla", I want to save files in this folder using a FileUpload control.
我该如何解决这个错误?问题是通向我名为“blablabla”的文件夹的路径名,我想使用 FileUpload 控件将文件保存在此文件夹中。
Any help would be greatly appreciated.
任何帮助将不胜感激。
回答by Steve
Because you should specify the destination path starting from the root folder of your site. Also there is no need to dupe the forward slash
因为您应该指定从站点根文件夹开始的目标路径。也没有必要欺骗正斜杠
FileUpload1.SaveAs(Server.MapPath("/blablabla/" + FileUpload1.FileName));
回答by Arawn
Try this ....
尝试这个 ....
protected void Button1_Click(object sender, EventArgs e)
{
string Img_name = FileUpload1.FileName;
string folder_path = Server.MapPath("~\userimages\");
FileUpload1.SaveAs(folder_path + Img_name);
}
回答by SanjayDVG
Try this,
尝试这个,
string filename = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
string Path = Server.MapPath("/" + filename);
FileUpload1.SaveAs(Path);