简体   繁体   English

如何从ASP.NET控制器提供文件?

[英]How to serve a file from an ASP.NET Controller?

I want to do this with c# asp.net, can you tell me the actual methods I need to use to handle this part: {get file from filesystem}? 我想使用c#asp.net做到这一点,您能告诉我处理这部分所需的实际方法吗:{从文件系统获取文件}?

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;
    }
}

There is direct support for this with FileResult , and Controller has a set of helpers: FileResult直接对此提供FileResultController有一组帮助器:

In your action: 在您的行动中:

return File(filename, contentType);

You have to have the file somewhere in your server, so just make a method to get that path and serve it through the controller like this : 您必须将文件放置在服务器中的某个位置,因此只需创建一个获取该路径并通过控制器将其提供服务的方法,如下所示:

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);

This is the solution I arrived at: 这是我到达的解决方案:

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;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ASP.NET MVC3 - 如何从另一个控制器提供View() - ASP.NET MVC3 - How to serve View() from another controller 在ASP.NET中提供静态文件 - Serve static file in ASP.NET 如何使用控制器使用 ASP.NET 向 http://domain_name.com 提供 POST 请求 - How to serve POST request to http://domain_name.com using ASP.NET using a controller AngularJS 从 ASP.NET MVC 控制器保存文件 - AngularJS Save File From ASP.NET MVC Controller 从Asp.net Web Api控制器获取文件名 - Get file name from Asp.net Web Api controller 如何将文件中的其他参数从 Angular 传递到 ASP.NET Core controller? - How to pass other parameter with file to ASP.NET Core controller from Angular? 如何将通用值列表从 ASP.NET MVC 中的控制器/视图传递到外部 js 文件 - How to pass generic list of values to external js file from controller/view in ASP.NET MVC 如何从Asp.Net核心控制器返回嵌入式静态html文件? - How to return an embedded static html file from an Asp.Net Core Controller? 如何在ASP.NET Core控制器中接收文件 - How to receive a file in ASP.NET Core controller 如何使用控制器打开pdf文件? ASP.NET MVC - How to open a pdf file using the controller? Asp.net MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM