IIS文件下载没有扩展名

时间:2013-12-19 19:14:25

标签: iis url-rewriting asp.net-web-api iis-8

我有一个.NET Web API 2应用程序,我已经连接api给我一个文件ID,然后从那里我从服务器获取唯一的文件。

示例:

   <a href="api/attachment/232-2323-2323-2323">Download</a>

我需要它是一个唯一的id,因为repo中可能有多个文件。但是,当我尝试单击下载按钮时,我得到了一个:

 HTTP Error 404.0 - Not Found
 The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

我认为重写规则可能是一个很好的动作,但我真的不想重写它,我只是想允许任何/ api / attachment,无论其他什么。

我已经有了一条重写规则,因为我的网页是一个单页面应用程序,可以直接响应Default.cshtml:

 <rewrite>
  <rules>
    <rule name="Default" stopProcessing="true">
      <match url="^(?!Lib|api|Assets|Views|Directives|Services|Controllers|signalr).*" />
      <action type="Rewrite" url="Default.cshtml" />
    </rule>
  </rules>
</rewrite>

关于实现这一目标的最佳途径的任何想法?

1 个答案:

答案 0 :(得分:0)

我能够通过创建iframe并将src设置为下载来解决:

 $("body").append('<iframe name="downloadFrame" id="download_iFrame" style="display:none;" src="" />');

然后在C#中设置标题如:

result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
相关问题