the*_*van 3 c# asp.net file-upload visual-studio-2010
我想在物理路径中上传文件,例如E:\Project\Folders
.
我在网上搜索下面的代码.
//check to make sure a file is selected
if (FileUpload1.HasFile)
{
//create the path to save the file to
string fileName = Path.Combine(Server.MapPath("~/Files"), FileUpload1.FileName);
//save the file to our local path
FileUpload1.SaveAs(fileName);
}
Run Code Online (Sandbox Code Playgroud)
但在这方面,我想像上面提到的那样给出我的物理路径.这该怎么做?
Server.MapPath("~/Files")
返回基于相对于应用程序的文件夹的绝对路径.领先者~/
告诉ASP.Net查看应用程序的根目录.
要使用应用程序之外的文件夹:
//check to make sure a file is selected
if (FileUpload1.HasFile)
{
//create the path to save the file to
string fileName = Path.Combine(@"E:\Project\Folders", FileUpload1.FileName);
//save the file to our local path
FileUpload1.SaveAs(fileName);
}
Run Code Online (Sandbox Code Playgroud)
当然,您不会在生产应用程序中硬编码路径,但这应该使用您描述的绝对路径保存文件.
关于在保存文件后定位文件(每条评论):
if (FileUpload1.HasFile)
{
string fileName = Path.Combine(@"E:\Project\Folders", FileUpload1.FileName);
FileUpload1.SaveAs(fileName);
FileInfo fileToDownload = new FileInfo( filename );
if (fileToDownload.Exists){
Process.Start(fileToDownload.FullName);
}
else {
MessageBox("File Not Saved!");
return;
}
}
Run Code Online (Sandbox Code Playgroud)