我找不到使用MVC Core下载文件的参考.
我们有一个exe文件供会员从我们的网站下载.过去我们已经把
<a href=(file path)> Download < /a>供我们的用户点击.我想在MVC Core中做同样的事情
<a href=@ViewData["DownloadLink"]> Download < /a>
Run Code Online (Sandbox Code Playgroud)
使用下载链接填充文件路径.
public class DownloadController : Controller
{
[HttpGet]
public IActionResult Index()
{
ViewData["DownloadLink"] = ($"~/Downloads/{V9.Version}.exe");
return View();
}
}
Run Code Online (Sandbox Code Playgroud)
`
链接<a href=@ViewData["DownloadLink"]> Download < /a>获取正确的路径,但单击时仅在地址栏中呈现路径.有没有简单的方法来设置下载链接?
我想在我的MVC应用程序中启用文件下载,而不是简单地使用超链接.我计划使用图像等,并使用jQuery使其可点击.目前我有一个简单的测试.
我找到了通过动作方法进行下载的解释,但遗憾的是该示例仍然有动作链接.
现在,我可以调用下载操作方法,但没有任何反应.我想我必须对返回值做一些事情,但我不知道是什么或如何.
这是动作方法:
public ActionResult Download(string fileName)
{
string fullName = Path.Combine(GetBaseDir(), fileName);
if (!System.IO.File.Exists(fullName))
{
throw new ArgumentException("Invalid file name or file does not exist!");
}
return new BinaryContentResult
{
FileName = fileName,
ContentType = "application/octet-stream",
Content = System.IO.File.ReadAllBytes(fullName)
};
}
Run Code Online (Sandbox Code Playgroud)
这是BinaryContentResult类:
public class BinaryContentResult : ActionResult
{
public BinaryContentResult()
{ }
public string ContentType { get; set; }
public string FileName { get; set; }
public byte[] Content { get; set; }
public override void ExecuteResult(ControllerContext context) …Run Code Online (Sandbox Code Playgroud) 我之前能够下载 zip 文件,但压缩发生在 ASP 服务器上。现在我们已将此操作更改为另一台服务器(进度)。
此时我收到一个表示 zip 文件的 Base64 编码字符串。但我怎样才能将这个字符串转换为 zip 文件。我之前使用的代码可以在下面找到,我可以重复使用代码吗?
MemoryStream outputStream = new MemoryStream();
outputStream.Seek(0, SeekOrigin.Begin);
using (ZipFile zip = new ZipFile())
{
foreach (string id in idArray)
{
string json = rest.getDocumentInvoice(Convert.ToInt32(id));
byte[] file = json.convertJsonToFile();
zip.AddEntry("invoice" + id + ".pdf", file);
}
zip.Save(outputStream);
}
outputStream.WriteTo(Response.OutputStream);
Response.AppendHeader("content-disposition", "attachment; filename=invoices.zip");
Response.ContentType = "application/zip";
return new FileStreamResult(outputStream, "application/zip");
Run Code Online (Sandbox Code Playgroud)
我不知道如何将字符串转换为 zip 文件。在此先感谢您的帮助
我在这里阅读如何从asp.net api下载文件的解决方案:https : //stackoverflow.com/a/3605510/1881147
因此,我按照以下代码创建API处理程序:
public HttpResponseMessage Post([FromBody]dynamic result)
{
var localFilePath = graphDataService.SaveToExcel(graphVm, graphImgUrl);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "testing.xlsx";
response.Content.Headers.ContentType = new MediaTypeHeaderValue("MS-Excel/xls");
return response;
//return graphDataService.SaveToExcel(graphVm, graphImgUrl);
}
Run Code Online (Sandbox Code Playgroud)
这是我的客户端:
$http({
url: '/msexcel',
method: 'post',
params: { param: JSON.stringify(param) }
}).success(function (data, status, headers, config) {
console.log(data); //HOW DO YOU HANDLE the response here so it downloads?
}).error(function (data, status, headers, config) {
console.log(status); …Run Code Online (Sandbox Code Playgroud) 我想在页面加载时列出文件夹中的所有文件。所以
为此,我刚刚创建了这样的
HTML代码
<input id="idd" type="file" multiple="true" class="file" data-preview-file-type="text">
Run Code Online (Sandbox Code Playgroud)
脚本
@section scripts{
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
url: '/Home/filesinfolder',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$.each(data, function (index, val) {
$('#idd').append('<li><a href="http://'+ val.Url +'" target="_new">' + val.Url + '</a></li>');
});
},
error: function (xhr, status, err) {
console.log('Response code:' + xhr.status);
console.log('[Error:' + err + '] ' + status);
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
控制器方法
public JsonResult filesinfolder()
{
DirectoryInfo salesFTPDirectory = null;
FileInfo[] files = null; …Run Code Online (Sandbox Code Playgroud) ajax ×2
asp.net-mvc ×2
c# ×2
javascript ×2
angularjs ×1
asp.net ×1
base64 ×1
content-type ×1
download ×1
file-io ×1
jquery ×1
json ×1
zip ×1