C# 如何从ASP.NET控制器提供文件服务?
我想用c#asp.net来实现这一点,你能告诉我处理这部分的实际方法吗:{从文件系统获取文件}C# 如何从ASP.NET控制器提供文件服务?,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我想用c#asp.net来实现这一点,你能告诉我处理这部分的实际方法吗:{从文件系统获取文件} ActionResult FunctionToServeFile(fileName, guid) { File result; var itemEntry = db.items.firstOrDefault(x => x.guid == guid); var itemPermissionsEntry = itemEntry.userPermissions
ActionResult FunctionToServeFile(fileName, guid)
{
File result;
var itemEntry = db.items.firstOrDefault(x => x.guid == guid);
var itemPermissionsEntry = itemEntry.userPermissions
.firstOrDefault(x => x.user == user.identity.name);
if(itemPermissionsEntry.view == true || itemPermissionsEntry.Modify == true)
{
result = {get file from filesystem}
return result;
}
else
{
return error;
}
}
有直接的支持,并且
Controller
有一组帮助程序:
在你的行动中:
return File(filename, contentType);
有直接的支持,并且
Controller
有一组帮助程序:
在你的行动中:
return File(filename, contentType);
您必须在服务器中的某个位置拥有该文件,因此只需创建一个方法来获取该路径,并通过控制器提供该路径,如下所示:
string thefile = SomeModelClass.SomeMethod(fileName, guid); // get full path to the file
var cd = new System.Net.Mime.ContentDisposition
{
FileName = Path.GetFileName(thefile),
Inline = false
};
Response.AppendHeader("Content-Disposition", cd.ToString());
string fileext = Path.GetExtension(thefile);
string mimeType = SomeMetodToMapextensionToMimeType(fileext); // You have to implement this by yourself
return File(System.IO.File.ReadAllBytes(thefile), mime);
您必须在服务器中的某个位置拥有该文件,因此只需创建一个方法来获取该路径,并通过控制器提供该路径,如下所示:
string thefile = SomeModelClass.SomeMethod(fileName, guid); // get full path to the file
var cd = new System.Net.Mime.ContentDisposition
{
FileName = Path.GetFileName(thefile),
Inline = false
};
Response.AppendHeader("Content-Disposition", cd.ToString());
string fileext = Path.GetExtension(thefile);
string mimeType = SomeMetodToMapextensionToMimeType(fileext); // You have to implement this by yourself
return File(System.IO.File.ReadAllBytes(thefile), mime);
这就是我得出的解决方案:
public ActionResult DownloadFile(string fileName, Guid guid)
{
Item item = db.Items.FirstOrDefault(x => x.ItemGUID == guid);
if (item == null)
return null;
List <SecurityMask> accessList = GetAccessRightsForItem(item.item_id,
ActiveDirectory.GetUserSID(User.Identity.Name));
bool hasAccess = false || (accessList.Contains(SecurityMask.View) || accessList.Contains(SecurityMask.Modify));
string filePath = Path.GetFullPath(Path.Combine(HttpRuntime.AppDomainAppPath,
"Files\\Items", guid.ToString(), fileName));
string mimeType = MimeMapping.GetMimeMapping(filePath);
bool fileExists = System.IO.File.Exists(filePath);
if (hasAccess && fileExists)
{
return File(System.IO.File.ReadAllBytes(filePath), mimeType);
}
return null;
}
public ActionResult下载文件(字符串文件名、Guid)
{
Item=db.Items.FirstOrDefault(x=>x.ItemGUID==guid);
如果(项==null)
返回null;
List accessList=GetAccessRightsForItem(item.item\u id,
GetUserSID(User.Identity.Name));
bool hasAccess=false | | |(accessList.Contains(SecurityMask.View)| | accessList.Contains(SecurityMask.Modify));
字符串filePath=Path.GetFullPath(Path.Combine(HttpRuntime.AppDomainAppPath,
“Files\\Items”,guid.ToString(),fileName));
字符串mimeType=MimeMapping.GetMimeMapping(文件路径);
bool fileExists=System.IO.File.Exists(文件路径);
if(hasAccess&&fileExists)
{
返回文件(System.IO.File.ReadAllBytes(filePath),mimeType);
}
返回null;
}
这是我得出的解决方案:
public ActionResult DownloadFile(string fileName, Guid guid)
{
Item item = db.Items.FirstOrDefault(x => x.ItemGUID == guid);
if (item == null)
return null;
List <SecurityMask> accessList = GetAccessRightsForItem(item.item_id,
ActiveDirectory.GetUserSID(User.Identity.Name));
bool hasAccess = false || (accessList.Contains(SecurityMask.View) || accessList.Contains(SecurityMask.Modify));
string filePath = Path.GetFullPath(Path.Combine(HttpRuntime.AppDomainAppPath,
"Files\\Items", guid.ToString(), fileName));
string mimeType = MimeMapping.GetMimeMapping(filePath);
bool fileExists = System.IO.File.Exists(filePath);
if (hasAccess && fileExists)
{
return File(System.IO.File.ReadAllBytes(filePath), mimeType);
}
return null;
}
public ActionResult下载文件(字符串文件名、Guid)
{
Item=db.Items.FirstOrDefault(x=>x.ItemGUID==guid);
如果(项==null)
返回null;
List accessList=GetAccessRightsForItem(item.item\u id,
GetUserSID(User.Identity.Name));
bool hasAccess=false | | |(accessList.Contains(SecurityMask.View)| | accessList.Contains(SecurityMask.Modify));
字符串filePath=Path.GetFullPath(Path.Combine(HttpRuntime.AppDomainAppPath,
“Files\\Items”,guid.ToString(),fileName));
字符串mimeType=MimeMapping.GetMimeMapping(文件路径);
bool fileExists=System.IO.File.Exists(文件路径);
if(hasAccess&&fileExists)
{
返回文件(System.IO.File.ReadAllBytes(filePath),mimeType);
}
返回null;
}