asp.net-mvc 从我网页内的链接下载文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19975689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download file from link inside my webpage
提问by user2978444
I have Webpage with table of objects.
我有带有对象表的网页。
One of my object properties is the file path, this file is locate in the same network. What i want to do is wrap this file path under link (for example Download) and after the user will click on this link the file will download into the user machine.
我的对象属性之一是文件路径,该文件位于同一网络中。我想要做的是将此文件路径包装在链接下(例如下载),在用户单击此链接后,文件将下载到用户计算机中。
so inside my table:
所以在我的桌子里面:
@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>
I created this download link:
我创建了这个下载链接:
<th width ="150"><p><b><a href="default.asp" target="_blank">Download</a></b></p></th>
I want this download link to wrap my file pathand click on thie link will lean to my controller:
我想要这个下载链接来包装我的file path,点击这个链接会倾向于我的控制器:
public FileResult Download(string file)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(file);
}
What i need to add to my code in order to acheive that ?
我需要在我的代码中添加什么才能实现这一目标?
回答by mehmet mecek
Return FileContentResult from your action.
从您的操作中返回 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;
}
And the download link,
还有下载链接,
<a href="controllerName/[email protected]" target="_blank">Download</a>
This link will make a get request to your Download action with parameter fileName.
此链接将使用参数 fileName 向您的下载操作发出获取请求。
EDIT: for not found files you can,
编辑:对于未找到的文件,您可以,
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;
}
回答by Rizvi Sarwar
In the view, write:
在视图中,写:
<a href="/ControllerClassName/DownloadFile?file=default.asp" target="_blank">Download</a>
In the controller, write:
在控制器中,写:
public FileResult DownloadFile(string file)
{
string filename = string.Empty;
Stream stream = ReturnFileStream(file, out filename); //here a backend method returns Stream
return File(stream, "application/force-download", filename);
}
回答by Hamid
This example works fine for me:
这个例子对我来说很好:
public ActionResult DownloadFile(string file="")
{
file = HostingEnvironment.MapPath("~"+file);
string contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
var fileName = Path.GetFileName(file);
return File(file, contentType,fileName);
}
View:
看法:
< script >
function SaveImg()
{
var fileName = "/upload/orders/19_1_0.png";
window.location = "/basket/DownloadFile/?file=" + fileName;
}
< /script >
<img class="modal-content" id="modalImage" src="/upload/orders/19_1_0.png" onClick="SaveImg()">

