c# - 如何在 Asp.Net 中设置上传文件的物理路径?

标签 c# asp.net visual-studio-2010 file-upload

我想上传物理路径中的文件,例如 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/

相关文章:

c# - 如何通过添加和删除从旧根检索到的节点来创建新根?

c++ - 我的 C++ 程序出现“....exe 已停止工作”Windows 7 错误

visual-studio - 对于 Visual Studio 2010 上 TFS 2008 中的所有冲突,我如何处理 "Take Source Branch Version"?

c# - 双击datagrid wpf如何找到标题或行

c# - Azure 服务总线 - 只有一个订阅者接收消息

c# - 如何将字符串转换为 Indian Money 格式?

c# - 当不需要 WCF DataMember 时

javascript - 使用 Javascript 连接到 WCF Web 服务

c# - 配置设置那些不能在较低级别的 web.config 文件中被覆盖

configuration - 像 resharper 一样在 Visual Studio 中自动添加关闭引号