我正在ASP.Net MVC中构建一个应用程序,并希望返回一个View
AND并向用户提供下载。是否有可能?现在,我可以使用
return View();
使用以下方式提供文件下载:
return File(FilePath, "text", "downloadFileName");
这样做的原因:会有一个复选框,指示“如果下载文件”。如果选中,单击按钮后,指定的内容将显示在屏幕上,并出现下载对话框。
任何帮助表示赞赏!
更新:最后,我选择在返回的中提供下载链接,该链接View
现在对应用程序非常有效。
请如下使用。
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模型返回视图。
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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句