如何设置物理路径上传Asp.Net文件? [英] How to set physical path to upload a file in Asp.Net?
问题描述
我要在物理路径的文件上传如 E:\\项目\\文件夹
I want to upload a file in a physical path such as E:\Project\Folders
.
我在网上搜索了以下code。
I got the below code by searching in the net.
//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);
}
但在这,我想正如我上面提到给我的物理路径。如何做到这一点?
But in that, I want to give my physical path as I mentioned above. How to do this?
推荐答案
使用Server.Mappath(〜/文件)
返回基于相对于文件夹的绝对路径你的申请。领先〜/
告诉ASP.Net来看看你的应用程序的根目录。
Server.MapPath("~/Files")
returns an absolute path based on a folder relative to your application. The leading ~/
tells ASP.Net to look at the root of your application.
要使用一个文件夹中的应用程序之外:
To use a folder outside of the application:
//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);
}
当然,你不会硬code在生产应用程序的路径,但这应该使用您所描述的绝对路径保存文件。
Of course, you wouldn't hardcode the path in a production application but this should save the file using the absolute path you described.
至于定位文件,一旦你已经救了它(每评论)
With regards to locating the file once you have saved it (per comments):
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;
}
}
这篇关于如何设置物理路径上传Asp.Net文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!