使用c#下载文件时不支持异常

本文关键字:不支持 异常 文件 下载 使用 | 更新日期: 2023-09-27 18:16:12

我想在我的ASP中下载一个文件。网络应用程序。我使用了以下代码片段:

try
{
    string s = fichier.GetFichierUrlById(_id);
    Response.ContentType = "application/" + Path.GetExtension(s); 
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + s);
    Response.TransmitFile(Server.MapPath("~/Downloads/"+s));
    Response.End();
}
catch { }

在这个例子中,我有一个文件setup.exe,我得到一个异常,如The format of the given path is not supported.

这个异常的原因是什么?我如何修复代码?

使用c#下载文件时不支持异常

检查S的值,从而检查"Server.MapPath("~/Downloads/"+ S)的返回值

你可能在S中有一些无效的字符,可能会把事情搞砸?

TransmitFile将假定在downloads子文件夹中存在一个物理文件。

如果不是这样,那么您需要自己做一些事情,比如将数据写入输出流。