3

我想在物理路径中上传文件,例如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);
}

但在那方面,我想给出我上面提到的物理路径。这个怎么做?

4

3 回答 3

7

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; 
    }
}
于 2012-05-07T09:50:06.173 回答
2

出色地,

您可以通过使用VirtualPathUtility来完成此操作

于 2012-05-07T09:50:17.787 回答
0
// 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);
}
于 2018-11-20T05:31:48.357 回答