[英]How to download a file in ASP.NET MVC using C#?
我正在做一项作业,并且停留在下载部分。 我可以上传文件。 但是,当我尝试在上传后下载同一文件时,出现错误(“访问被拒绝”)。
这是我编写的代码:
[HttpGet]
public FileResult Download (string fileName)
{
var permissionSet = new PermissionSet(PermissionState.None);
var writePermission = new FileIOPermission(FileIOPermissionAccess.Write, @"D:\Assignment\Ment\Ment\Photos");
permissionSet.AddPermission(writePermission);
var FileVirtualPath = Server.MapPath("~/Photos/" + fileName); //"~/Photos/" + fileName;
return new FilePathResult(@"D:\Assignment\Ment\Ment\Photos", "application/octet-stream");
}
试试这样的事情:
public FileResult Download(string ImageName)
{
return File(“<your path>” + ImageName, System.Net.Mime.MediaTypeNames.Application.Octet);
}
另外,请访问此链接:
你可以做
[HttpGet]
public virtual ActionResult Download(string fileName)
{
//fileName should be like "photo.jpg"
string fullPath = Path.Combine(Server.MapPath("~/Photos"),fileName);
return File(fullPath, "application/octet-stream", fileName);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.