我想上传一个文件,如物理路径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;
}
}
好,
您可以通过完成此VirtualPathUtility
// Fileupload1 is ID of Upload file
if (Fileupload1.HasFile)
{
// Take one variable 'save' for store Destination folder path with file name
var save = Server.MapPath("~/Demo_Images/" + Fileupload1.FileName);
Fileupload1.SaveAs(save);
}