C# Razor网站下载自定义扩展文件404错误
C# Razor Website Download custom extension file 404 error
我有一个简单的锚 link 需要从服务器下载文件。文件名为:“RT350Z_cfg.modbus”。我相信服务器无法识别文件类型,所以它给我一个 404 错误。
文件的确切路径是:
~/uploads/UNITS/docs/VFD/RT350Z_cfg.modbus
我的 cshtml 文件只有这个:
<a target="_blank" href="@(VirtualPathUtility.ToAbsolute("~/uploads/UNITS/docs/VFD/RT350Z_cfg.modbus"))">Download File</a>
当我点击 link 时,出现 404 错误未找到。我已经三次检查文件是否存在于服务器的那个位置。
然后我将扩展名修改为 *.txt
文件并修改了 hyperlink 以查找 txt 文件。
<a target="_blank" href="@(VirtualPathUtility.ToAbsolute("~/uploads/UNITS/docs/VFD/RT350Z_cfg.txt"))">Download File</a>
当我物理地将扩展名修改为 *.txt
文件,然后更改 hyperlink - 文件下载得很好。
如何允许我的 Web 应用程序下载自定义文件扩展名?
某些版本的 IIS 不提供 'unknown' 文件。
您可以配置 web.config 以根据需要处理文件:
<system.webServer>
<staticContent>
<mimeMap fileExtension=".modbus" mimeType="text/plain" />
或二进制:
<mimeMap fileExtension=".modbus" mimeType="application/octet-stream" />
您可以在 MSDN 阅读更多相关信息。
通常,我在控制器中创建一个 Action returns a FileContentResult
并在视图中简单地创建一个 ActinLink
以指向给它。这是更多 MVC 下载文件的方式。另外,您可以完全控制它。
public ActionResult DownloadFile()
{
//Use your own way to read the file. Or even you can pass a file stream to it.
var file = System.IO.File.ReadAllBytes(Server.MapPath("~/App_Data/DataFile.anyExt"));
return File(file, "contentType", "yourDesiredFileName.anyExtOfYourChoice");
}
并在视图中使用以下内容:
@Html.ActionLink("Click me to Download", "DownloadFile")
我有一个简单的锚 link 需要从服务器下载文件。文件名为:“RT350Z_cfg.modbus”。我相信服务器无法识别文件类型,所以它给我一个 404 错误。
文件的确切路径是:
~/uploads/UNITS/docs/VFD/RT350Z_cfg.modbus
我的 cshtml 文件只有这个:
<a target="_blank" href="@(VirtualPathUtility.ToAbsolute("~/uploads/UNITS/docs/VFD/RT350Z_cfg.modbus"))">Download File</a>
当我点击 link 时,出现 404 错误未找到。我已经三次检查文件是否存在于服务器的那个位置。
然后我将扩展名修改为 *.txt
文件并修改了 hyperlink 以查找 txt 文件。
<a target="_blank" href="@(VirtualPathUtility.ToAbsolute("~/uploads/UNITS/docs/VFD/RT350Z_cfg.txt"))">Download File</a>
当我物理地将扩展名修改为 *.txt
文件,然后更改 hyperlink - 文件下载得很好。
如何允许我的 Web 应用程序下载自定义文件扩展名?
某些版本的 IIS 不提供 'unknown' 文件。 您可以配置 web.config 以根据需要处理文件:
<system.webServer>
<staticContent>
<mimeMap fileExtension=".modbus" mimeType="text/plain" />
或二进制:
<mimeMap fileExtension=".modbus" mimeType="application/octet-stream" />
您可以在 MSDN 阅读更多相关信息。
通常,我在控制器中创建一个 Action returns a FileContentResult
并在视图中简单地创建一个 ActinLink
以指向给它。这是更多 MVC 下载文件的方式。另外,您可以完全控制它。
public ActionResult DownloadFile()
{
//Use your own way to read the file. Or even you can pass a file stream to it.
var file = System.IO.File.ReadAllBytes(Server.MapPath("~/App_Data/DataFile.anyExt"));
return File(file, "contentType", "yourDesiredFileName.anyExtOfYourChoice");
}
并在视图中使用以下内容:
@Html.ActionLink("Click me to Download", "DownloadFile")