我正在ASP.Net MVC中构建一个应用程序,并希望返回View
并向用户提供下载。可能吗?现在,我可以使用
return View();
使用以下方式提供文件下载:
return File(FilePath, "text", "downloadFileName");
原因:将有一个复选框,指示“是否下载文件”。如果选中,则单击按钮时,屏幕上将显示指定的内容,并显示下载对话框。
感谢任何帮助!
更新:
最后,我选择在返回的View
中提供下载链接,该链接现在适用于该应用。
答案 0 :(得分:2)
请使用如下。
ViewData["text"] = "text that you need to return";
ViewData["FileName"] = "Name of the file that you need to return";
ViewData["Filepath"] = "Path of the file that you need to return";
return View();
在您的视图中,您可以使用它们,如下所示
@{
var text = ViewData["text"];
var filename = ViewData["FileName"];
var filePath = ViewData["Filepath"];
}
如果您需要在不使用ViewData或ViewBage的情况下完成,请按照以下代码进行操作。
需要做3个步骤。
第1步: 为它创建一个模型类。 我的型号代码
public class FileDetails
{
public string Text { get; set; }
public string FileName { get; set; }
public string Filepath { get; set; }
}
步骤2:使用FileDetails Model返回视图的控制器代码。
FileDetails Details = new FileDetails();
Details.Text = "text that you need to return";
Details.FileName = "Name of the file that you need to return";
Details.Filepath = "Path of the file that you need to return";
return View("ViewName", Details);
步骤3:您的视图必须包含FileDetails模型标题。像下面
@model YourProjectName.Models.FileDetails
以上代码必须位于您需要使用这些详细信息的视图页面的顶部。
我的观看代码
@{
var text = Model.Text;
var filename = Model.FileName;
var filePath = Model.Filepath;
}