gpt4 book ai didi

javascript - 为什么在 ASP.NET MVC 中我得到 JSON 而不是 FileDownload?

转载 作者:行者123 更新时间:2023-11-28 07:50:51 36 4
gpt4 key购买 nike

我希望能够从我的网站下载附件(我需要使其在 IE 中运行)所以在 html 中我有:

<a href="api/attachments/DownloaAttachment?id={{attachment.Id}}" target="_blank">
Download Image
</a>
<!-- {{attachment.Id}} - it's because I use AngularJS :) -->

在 Controller 中:

        [HttpGet]
public FileContentResult DownloaAttachment(int id)
{
var att = GetAttachmentById(id);
byte[] fileBytes = att.Content.Content;
var response = new FileContentResult(fileBytes, MediaTypeNames.Application.Octet);
response.FileDownloadName = att.FileName;
return response;
}

但是当我单击“下载图像”时 - 我有一个新窗口,并且 Controller 以 json 形式进行响应,例如:

{"FileContents":"/9j/4.. some bytes..ZK9k=","ContentType":"application/octet-stream","FileDownloadName":"Cover.jpg"}

但是我不需要那个 JSON,我需要能够将附件作为文件下载到用户的计算机上。我怎样才能使它成为简单的文件下载?我做错了什么?

最佳答案

尝试更改此行:

var response = new FileContentResult(fileBytes, MediaTypeNames.Application.Octet);

至:

var response = new FileContentResult(fileBytes, "image/jpeg");

关于javascript - 为什么在 ASP.NET MVC 中我得到 JSON 而不是 FileDownload?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26820127/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com