【问题标题】:Upload / Download From Virtual Directories in ASP.NET C#从 ASP.NET C# 中的虚拟目录上传/下载
【发布时间】:2015-01-22 09:40:33
【问题描述】:

我希望能够从我的 ASP.NET WebApp 中的特定文件夹上传/下载文件, 现在由于应用程序驻留在 C:/ 目录中,我不想占用该分区中的空间,所以我添加了一个指向文件夹 E:/Docs/Emps/ 的虚拟目录 现在我可以正常上传了.. 但我在 E:/Docs/Emps/ 中找不到文件,它不存在, 当我搜索它们时,结果带有我在 IIS 中创建的虚拟文件夹中的 URL, 现在当我开始实现下载部分时..我根本无法下载它, 我找不到/访问[如果我设法找到它们]文件 在 Google 上呆了太多时间后,我尝试使用 WebClient,但我遇到了这个异常

消息
------------
在 WebClient 请求期间发生异常。
-----------
内部异常
-----------
System.NotSupportedException:不支持给定路径的格式。在 System.IO.FileStream.Init(字符串路径、FileMode 模式、FileAccess 访问、Int32 权限、Boolean useRights、FileShare 共享、Int32 bufferSize、FileOptions 选项、SECURITY_ATTRIBUTES secAttrs、String msgPath、Boolean bFromProxy、Boolean useLongPath、Boolean checkHost)在 System System.Net.WebClient.DownloadFile(Uri address, String fileName) 的 .IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
-----------
堆栈跟踪
------------
在 System.Net.WebClient.DownloadFile(Uri address, String fileName) at System.Net.WebClient.DownloadFile(String address, String fileName) at APC_ERP.BusinessCore_EmployeesDocumentsCenter.imgbtnDownloadDocument_Click(Object sender, ImageClickEventArgs e)

这是我正在使用的下载代码

WebClient Client = new WebClient();
string Path = Server.MapPath("~/"+(sender as ImageButton).CommandArgument);
string[]File=Path.Split('/');
string Destination = @"C:\" + File[File.Length - 1];
Client.DownloadFile(Path, Destination);
Client.Dispose();

【问题讨论】:

    标签: c# asp.net .net webforms


    【解决方案1】:

    变量Path 的内容似乎不正确。首先,不支持正斜杠,其次波浪号 (~) 可能是一个问题 - 不过我不确定后者。由于我们看不到您的数据,您应该调试并检查此路径变量以及Destination

    另外,不使用+,建议使用静态Combine 方法进行路径相关操作。示例:

    Path.Combine(@"\\root\", (sender as ImageButton).CommandArgument);
    

    【讨论】:

      猜你喜欢
      • 2016-05-23
      • 1970-01-01
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-16
      • 2022-11-23
      • 1970-01-01
      相关资源
      最近更新 更多