[英]How to set physical path to upload a file in Asp.Net?
I want to upload a file in a physical path such as E:\\Project\\Folders
. 我想在物理路径中上传文件,例如
E:\\Project\\Folders
。
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("~/Files")
returns an absolute path based on a folder relative to your application. Server.MapPath("~/Files")
根据相对于您的应用程序的文件夹返回绝对路径。 The leading ~/
tells ASP.Net to look at the root of your application. 领先
~/
告诉ASP.Net查看应用程序的根目录。
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);
}
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;
}
}
Well, 好,
you can accomplish this by using the VirtualPathUtility 您可以使用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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.