Asp.Net Core Mvc下载处理__Asp.Net Core Mvc文件下载

Asp.Net Core Mvc下载处理__Asp.Net Core Mvc文件下载

 

1、服务器生成下载数据

//下载
public IActionResult One()
{
    //从程序重生成的数据 字符串

    string str = string.Empty;

    for (int i = 0; i < 1000; i++)
    {
        str += DateTime.Now.ToString() + "---->" + i + "\r\n"; 
    }

    byte[] result = Encoding.UTF8.GetBytes(str);

    //文件内容,文件MIME格式,下载的文件名字
    return File(result,"text/plain","测试文件.txt");
}

2、服务器文件下载

public IActionResult Three()
{
    //当前网站的相对目录
    return File("~/css/site.css","text/plain","当前网站的文件.css");
}

3、服务器磁盘文件下载

public IActionResult Fourth()
{
    //下载服务器磁盘上的文件
    return PhysicalFile(@"H:\桌面\test\1.jpg", "image/jpeg","服务器磁盘.jpg");
}

 

4、配置mime类型下载

app.UseStaticFiles(new StaticFileOptions {
               ContentTypeProvider=new FileExtensionContentTypeProvider(new Dictionary<string, string> {
                   {
                       ".apk","application/vnd.android.package-archive"
                   },
                   {
                      //其它它类型
                   }
               })
            });

 

更多:

Asp.Net Core 跨域处理_Ajax 跨域处理整理

asp.netCore3.0区域和路由配置变化

在Asp.Net Core 3.0中如何使用 Newtonsoft.Json 库序列化数据

posted @ 2021-12-24 19:01  天马3798  阅读(554)  评论(0编辑  收藏  举报