asp.net核心上的静态文件 [英] Static files on asp.net core

查看:53
本文介绍了asp.net核心上的静态文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 ASP.NET Core 2.0 Web应用程序上启用静态文件.我在名为updater的文件夹中有一堆文件,该文件夹位于 wwwroot 文件夹之外.为了允许访问它们,我添加了

I am trying to enable static files on an ASP.NET Core 2.0 web application. I have a bunch of files in a folder called updater which resides outside the wwwroot folder. To allow access to them I added

app.UseStaticFiles(new StaticFileOptions()
{
    ServeUnknownFileTypes = true,
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), @"TestUpdater")
    ),
    RequestPath = new PathString("/Updater")
});

这使其他程序能够通过调用URL来获取其文件.问题是所有文件都需要下载而不是提供.有一个txt文件.如何只允许下载而不提供服务?

This lets a different program to be able to get its files by calling the urls. The issue is all the files need to be downloaded instead of served. There is one txt file. How do I allow only download instead of it being served?

推荐答案

您所描述的服务"和下载"文件之间的唯一区别在于,在一种情况下,浏览器会将文件下载到一个临时位置并在其中显示窗口(内联),另一个浏览器会询问用户将文件保存到永久位置(附件)的位置.

The only difference between "serving" and "downloading" files as you describe is that in one instance the browser downloads the file to a temporary location and displays it in the window (inline) while the other the browser will ask a user where to save the file to a permanent location (attachment).

如果您所引用的其他程序正在直接与服务器联系以获取这些文件,则没关系.例如,使用HttpClient,您根本不需要更改静态文件中间件.

If the other programs you're referring to are contacting the server for these files directly it shouldn't matter. For example using HttpClient, you don't have to change your static file middleware at all.

如果您希望浏览器提示用户保存文件,即使该文件是可识别的内容类型,请尝试将Content-Disposition响应标头设置为附件".要从Startup.cs配置中执行此操作,请将您的StaticFileOptions修改为使用类似这样的内容:

If you want the browser to prompt the user to save the file even if it's a recognized content type, try setting the Content-Disposition response header to "attachment". To do this from your Startup.cs config, modify your StaticFileOptions to use something like this:

new StaticFileOptions()
{
    OnPrepareResponse = context =>
    {
        context.Context.Response.Headers["Content-Disposition"] = "attachment";
    }
}

这篇关于asp.net核心上的静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆