如何从 MVC 5 中的 FileResult 和锚标记下载文件?
How To Download A file From FileResult and anchor tag in MVC 5?
这是我的文件上传和下载代码
[HttpPost]
public ActionResult Save(Rent Rent , FileUpload upload, HttpPostedFileBase file)
{
if (Rent.Id == 0)
_Context.Rent.Add(Rent);
else
{
var rentInDb = _Context.Rent.Single(c => c.Id == Rent.Id);
rentInDb.tenantId = Rent.tenantId;
rentInDb.unitId = Rent.unitId;
rentInDb.startDate = Rent.startDate;
rentInDb.endDate = Rent.endDate;
rentInDb.Amount = Rent.Amount;
rentInDb.leaseStatus = Rent.leaseStatus;
}
_Context.SaveChanges();
var rent = _Context.Rent.Single(r => r.Id == Rent.Id);
var up = Request.Files["file"];
if (up.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var guid = Guid.NewGuid().ToString();
var path = Path.Combine(Server.MapPath("~/uploads"), guid + fileName);
file.SaveAs(path);
string fl = path.Substring(path.LastIndexOf("\"));
string[] split = fl.Split('\');
string newpath = split[1];
string imagepath = "~/uploads/" + newpath;
upload.length = imagepath;
upload.Rent = rent;
_Context.FileUpload.Add(upload);
_Context.SaveChanges();
}
return RedirectToAction("leaseStatus", "Home");
}
public ActionResult Downloads(int id)
{
var fl = _Context.FileUpload.Where(f => f.rentId == id);
var up = Request.Files["file"];
return View(fl );
}
public FileResult Download(string ImageName)
{
var FileVirtualPath = "~/uploads/" + ImageName;
return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));
}
这是我的观点!!
@model IEnumerable<mallform.Models.FileUpload>
@{
ViewBag.Title = "Downloads";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Downloads</h2>
@foreach (var file in Model)
{
<a href=@file.length target="_blank">Download</a>
}
这显示标准 404.0 和有时隐藏元素错误 :( 请帮忙。在我的代码中,它包括上传文件代码,
然后有一个下载操作导致我下载视图,在下载视图中,我有一个 link 按文件结果下载文件。但它总是向我显示错误。请告诉我路径是否有问题或发生了什么?
文件结果操作应该是这样的。
public FileResult Download(string ImageName)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath(@"~/uploads/"+ImageName));
string fileName = "myfile."+Path.GetExtension(Server.MapPath(@"~/uploads/"+ImageName));
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
Error Code 404 means the url is not found by your anchor tag. https://en.wikipedia.org/wiki/HTTP_404
您需要将 ImageName 参数从锚标记传递到控制器。你可以这样做:
查看
@model IEnumerable<mallform.Models.FileUpload>
@{
ViewBag.Title = "Downloads";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Downloads</h2>
@foreach (var file in Model)
{
@Html.ActionLink(
"Download File", // Anchor Text
"Download", // Action Name
"Controller Name", // Controller Name
new {
ImageName= "Pass the value of imagename parameter"
},
null // Html Attributes
)
}
控制器
public FileResult Download(string ImageName)
{
//var FileVirtualPath = "~/uploads/" + ImageName;
//If using Physical Path
//var FileVirtualPath = HttpContext.Current.Request.MapPath("~/uploads/" + ImageName);
//If using Virtual Path
var FileVirtualPath = HttpContext.Current.Server.MapPath("~/uploads/" + ImageName);
return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));
}
这是我的文件上传和下载代码
[HttpPost]
public ActionResult Save(Rent Rent , FileUpload upload, HttpPostedFileBase file)
{
if (Rent.Id == 0)
_Context.Rent.Add(Rent);
else
{
var rentInDb = _Context.Rent.Single(c => c.Id == Rent.Id);
rentInDb.tenantId = Rent.tenantId;
rentInDb.unitId = Rent.unitId;
rentInDb.startDate = Rent.startDate;
rentInDb.endDate = Rent.endDate;
rentInDb.Amount = Rent.Amount;
rentInDb.leaseStatus = Rent.leaseStatus;
}
_Context.SaveChanges();
var rent = _Context.Rent.Single(r => r.Id == Rent.Id);
var up = Request.Files["file"];
if (up.ContentLength > 0) {
var fileName = Path.GetFileName(file.FileName);
var guid = Guid.NewGuid().ToString();
var path = Path.Combine(Server.MapPath("~/uploads"), guid + fileName);
file.SaveAs(path);
string fl = path.Substring(path.LastIndexOf("\"));
string[] split = fl.Split('\');
string newpath = split[1];
string imagepath = "~/uploads/" + newpath;
upload.length = imagepath;
upload.Rent = rent;
_Context.FileUpload.Add(upload);
_Context.SaveChanges();
}
return RedirectToAction("leaseStatus", "Home");
}
public ActionResult Downloads(int id)
{
var fl = _Context.FileUpload.Where(f => f.rentId == id);
var up = Request.Files["file"];
return View(fl );
}
public FileResult Download(string ImageName)
{
var FileVirtualPath = "~/uploads/" + ImageName;
return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));
}
这是我的观点!!
@model IEnumerable<mallform.Models.FileUpload>
@{
ViewBag.Title = "Downloads";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Downloads</h2>
@foreach (var file in Model)
{
<a href=@file.length target="_blank">Download</a>
}
这显示标准 404.0 和有时隐藏元素错误 :( 请帮忙。在我的代码中,它包括上传文件代码, 然后有一个下载操作导致我下载视图,在下载视图中,我有一个 link 按文件结果下载文件。但它总是向我显示错误。请告诉我路径是否有问题或发生了什么?
文件结果操作应该是这样的。
public FileResult Download(string ImageName)
{
byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath(@"~/uploads/"+ImageName));
string fileName = "myfile."+Path.GetExtension(Server.MapPath(@"~/uploads/"+ImageName));
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}
Error Code 404 means the url is not found by your anchor tag. https://en.wikipedia.org/wiki/HTTP_404
您需要将 ImageName 参数从锚标记传递到控制器。你可以这样做:
查看
@model IEnumerable<mallform.Models.FileUpload>
@{
ViewBag.Title = "Downloads";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Downloads</h2>
@foreach (var file in Model)
{
@Html.ActionLink(
"Download File", // Anchor Text
"Download", // Action Name
"Controller Name", // Controller Name
new {
ImageName= "Pass the value of imagename parameter"
},
null // Html Attributes
)
}
控制器
public FileResult Download(string ImageName)
{
//var FileVirtualPath = "~/uploads/" + ImageName;
//If using Physical Path
//var FileVirtualPath = HttpContext.Current.Request.MapPath("~/uploads/" + ImageName);
//If using Virtual Path
var FileVirtualPath = HttpContext.Current.Server.MapPath("~/uploads/" + ImageName);
return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));
}