我想上传物理路径中的文件,例如 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);
}
但在那方面,我想给出我上面提到的物理路径。如何做到这一点?
最佳答案
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);
}
当然,您不会在生产应用程序中对路径进行硬编码,但这应该使用您描述的绝对路径来保存文件。
关于保存文件后的定位(根据评论):
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;
}
}
关于c# - 如何在 Asp.Net 中设置上传文件的物理路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10479942/