asp.net-mvc – 从我的网页链接下载文件
发布时间:2020-05-23 05:55:06 所属栏目:asp.Net 来源:互联网
导读:我有页面与对象表. 我的一个对象属性是文件路径,这个文件位于同一个网络中.我想做的是将此文件路径包装在链接下(例如下载),用户点击此链接后,该文件将下载到用户机器中. 所以在我的桌子里面 @foreach (var item in Model) { tr th width =150pba href=defaul
|
我有页面与对象表. 我的一个对象属性是文件路径,这个文件位于同一个网络中.我想做的是将此文件路径包装在链接下(例如下载),用户点击此链接后,该文件将下载到用户机器中. 所以在我的桌子里面 @foreach (var item in Model)
{
<tr>
<th width ="150"><p><b><a href="default.asp" target="_blank">Download</a></b></p></th>
<td width="1000">@item.fileName</td>
<td width="50">@item.fileSize</td>
<td bgcolor="#cccccc">@item.date<td>
</tr>
}
</table>
我创建了这个下载链接: <th width ="150"><p><b><a href="default.asp" target="_blank">Download</a></b></p></th> 我想要这个下载链接包装我的文件路径,点击链接将倾向于我的控制器: public FileResult Download(string file)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(file);
}
我需要添加到我的代码才能实现? 解决方法从您的操作返回FileContentResult.public FileResult Download(string file)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(file);
var response = new FileContentResult(fileBytes,"application/octet-stream");
response.FileDownloadName = "loremIpsum.pdf";
return response;
}
和下载链接, <a href="controllerName/Download?file=@item.fileName" target="_blank">Download</a> 此链接将使用参数fileName获取您的下载操作的请求. 编辑:对于未找到的文件,您可以, public ActionResult Download(string file)
{
if (!System.IO.File.Exists(file))
{
return HttpNotFound();
}
var fileBytes = System.IO.File.ReadAllBytes(file);
var response = new FileContentResult(fileBytes,"application/octet-stream")
{
FileDownloadName = "loremIpsum.pdf"
};
return response;
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – Umbraco有没有好的电子商务扩展/插件?
- asp.net-mvc – VaryByParam =“*”是否也读取了RouteData.
- asp.net中.aspx页面中各种符号的含义
- asp.net – 我找不到在visual studio 2010中从工具菜单生成
- asp.net-mvc – 使用没有ORM的ASP.NET MVC
- 如何组合WebResource.axd和ScriptResource.axd文件,以减少对
- asp.net-mvc – 如何使用MVC 4上传大文件?
- .net – asp:GridView文本框始终返回空值
- asp.net-mvc-3 – 如何将int数组传递给RouteValueDictionar
- asp.net-mvc – 如何阻止Elmah伐木?
推荐文章
站长推荐
热点阅读
