下载名称为

时间:2016-02-15 13:49:43

标签: javascript c#

我正在处理一个具有上传和下载文件功能的C#网络系统。其中一些文件的名称或文件夹中恰好有“+”字符。

我发现“+”在URL中是有问题的,因为它表示连接,而加号应编码为%2b。

所以我尝试设置newUrl =url.string.Replace("+", "%2b"),然后执行:

System.Net.WebClient req = new System.Net.WebClient();
req.UseDefaultCredentials = true;
byte[] buffer = req.DownloadData(newUrl);

最终出现了下载失败和应用程序异常消息

  

“远程服务器返回错误:(404)Not Found。”

此外,尝试从浏览器访问该文件,或者 http://repository.mysite.figure+numbers.pdfhttp://repository.mysite.figure%2bnumbers.pdf 以错误404结束。

似乎没有出路。寻找文件“draw%2bnumbers.pdf”会失败,因为它不存在,但另一方面“draw + numbers.pdf”也会失败,因为没有“drawnumbers.pdf”。

除了重命名存储库中的所有文件和文件夹外,是否有解决此问题的方法?

1 个答案:

答案 0 :(得分:2)

有一个解决方案。检查此link

IIS默认情况下不允许双重转义,这会阻止 + 字符在网址中正确显示。

更新答案:

我们需要在webconfig的安全部分添加<requestFiltering allowDoubleEscaping="true" />并重置IIS一次以使其正常工作。 像这样:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <security>
      <requestFiltering allowDoubleEscaping="true" />
    </security>
  </system.webServer>
</configuration>