C# 如何使用FileResult下载文件?
我的视图中有一个带有ActionLink按钮“下载”的列表,我希望他们在单击链接时下载一个文件。该文件位于我的项目中的地图中 查看:C# 如何使用FileResult下载文件?,c#,asp.net-mvc,download,C#,Asp.net Mvc,Download,我的视图中有一个带有ActionLink按钮“下载”的列表,我希望他们在单击链接时下载一个文件。该文件位于我的项目中的地图中 查看: <div id="right-column-links"> <h2>Your active links</h2> @if (lstLinks.Count == 0) { <p>You have no active links yet.</p> } e
<div id="right-column-links">
<h2>Your active links</h2>
@if (lstLinks.Count == 0)
{
<p>You have no active links yet.</p>
}
else
{
<table>
@foreach (var item in lstLinks)
{
<tr>
<td>@Html.DisplayFor(model => item.Url)</td>
<td>@Html.ActionLink("Put inactive", "LinkInActive", new { linkid=item.LinkId }, new { onclick = "return confirm('Are you sure you want this link inactive?');" })</td>
<td>@Html.ActionLink("Download Qrcode", "DownloadQrcode", new { linkid=item.LinkId })</td>
</tr>
}
</table>
}
</div>
[HttpPost]
public FileResult DownloadQrcode(int linkid)
{
Qrcode Qrcode = DbO.getQrcodebyLinkId(linkid);
string image = Server.MapPath("~") + "\\Qrcodes\\" + Qrcode.Image;
string contentType = "image/jpg";
return File(image, contentType, "Qrcode-" + Qrcode.QrcodeId);
}
linkid来自列表中选定的链接。然后在数据库中查找与linkid匹配的qrcode。从这个qrcode对象我得到了图像名称。示例(qrcode-1337)。那我就不知道该怎么办了。我查找存储项目的路径,并将地图Qrcodes附加到该路径(存储所有图像的位置)和图像名称。这会给我一个他找不到的链接
地图位置:
C:\Users\stage\Desktop\Immo QR\Immo QR\Immo QR\Qrcodes
这似乎不起作用。我不确定应该如何使用FileResult。有人能解释一下吗?还是给我另一条路
编辑:
<div id="right-column-links">
<h2>Your active links</h2>
@if (lstLinks.Count == 0)
{
<p>You have no active links yet.</p>
}
else
{
<table>
@foreach (var item in lstLinks)
{
<tr>
<td>@Html.DisplayFor(model => item.Url)</td>
<td>@Html.ActionLink("Put inactive", "LinkInActive", new { linkid=item.LinkId }, new { onclick = "return confirm('Are you sure you want this link inactive?');" })</td>
<td>@Html.ActionLink("Download Qrcode", "DownloadQrcode", new { linkid=item.LinkId })</td>
</tr>
}
</table>
}
</div>
[HttpPost]
public FileResult DownloadQrcode(int linkid)
{
Qrcode Qrcode = DbO.getQrcodebyLinkId(linkid);
string image = Server.MapPath("~") + "\\Qrcodes\\" + Qrcode.Image;
string contentType = "image/jpg";
return File(image, contentType, "Qrcode-" + Qrcode.QrcodeId);
}
一位用户建议我将这些图像放在App_数据文件中,我将其放在地图下
要保存文件,我使用以下代码:
字符串路径=Server.MapPath(“~”)
如果我使用“~\App\u Data\Qrcodes\qrcode-”而不是上面提到的,它也不起作用
我仍然收到此错误:“/”应用程序中的服务器错误。找不到资源
解决方案:
<div id="right-column-links">
<h2>Your active links</h2>
@if (lstLinks.Count == 0)
{
<p>You have no active links yet.</p>
}
else
{
<table>
@foreach (var item in lstLinks)
{
<tr>
<td>@Html.DisplayFor(model => item.Url)</td>
<td>@Html.ActionLink("Put inactive", "LinkInActive", new { linkid=item.LinkId }, new { onclick = "return confirm('Are you sure you want this link inactive?');" })</td>
<td>@Html.ActionLink("Download Qrcode", "DownloadQrcode", new { linkid=item.LinkId })</td>
</tr>
}
</table>
}
</div>
[HttpPost]
public FileResult DownloadQrcode(int linkid)
{
Qrcode Qrcode = DbO.getQrcodebyLinkId(linkid);
string image = Server.MapPath("~") + "\\Qrcodes\\" + Qrcode.Image;
string contentType = "image/jpg";
return File(image, contentType, "Qrcode-" + Qrcode.QrcodeId);
}
有了这段代码,它就可以工作了
public FileStreamResult DownloadQrcode(int linkid)
{
Qrcode Qrcode = DbO.getQrcodebyLinkId(linkid);
string path = Server.MapPath("~");
Stream image = new FileStream(path + "\\App_Data\\Qrcodes\\" + Qrcode.Image + ".jpg", FileMode.Open);
return File(image, "image/jpeg");
}
你的方法是正确的 我认为文件的路径不正确 如果使用
~\\Qrcodes\\filename
它将转换为\\Qrcodes\\filename
还请记住,在大多数情况下,IIS作为单独的用户运行,不像普通用户那样有主目录
我建议您将Qrcodes移动到AppData文件夹或AppGlobalResources文件夹
如果不想这样做,则需要提供Qrcodes文件夹的绝对路径。尝试将
字符串图像
行更改为流图像
这将有助于理解您是否无法读取文件。您的
返回文件
行将接收一个没有问题的流。我也按照您的要求做了,但不幸的是没有成功。更新了我的问题!如何使用流对象?我似乎无法让它工作。Stream image==new FileStream(fileName,FileMode.Open)
我收到了相同的错误。这意味着他找不到文件?@nanouponete在Stream image=…
上设置一个断点,并在调试模式下运行。