ASP.NET Core downloads the file and informs the browser to download the image

To notify the browser to download the picture, the main thing is to set the response header on the server side

Insert picture description here

Content-Disposition: attachment; filename=20210201160550_567_d9a7.jpg; filename*=UTF-8''20210201160550_567_d9a7.jpg

ASP.NET Core download files; download pictures



//虚拟路径下载 图片
[HttpGet]
public ActionResult DownImg1()
{
    
    
	//虚拟路径下载 wwwroot下面的文件
	return File("/upload/b4.jpg", "image/jpeg", "123.jpg");
}

//物理地址下载 图片
[HttpGet]
public ActionResult DownImg2()
{
    
    
	return PhysicalFile(@"D:\壁纸\b3.jpg", "image/jpeg", "123.jpg");
}

//字节数组下载 图片
[HttpGet]
public async Task<ActionResult> DownImg3()
{
    
    
	string imgUrl="http://www.baidu.com/img/4455.jpg";
	System.Net.WebClient webClient = new System.Net.WebClient();
	byte[] buffe = await webClient.DownloadDataTaskAsync(imgUrl);
	return File(buffe, "image/jpeg","123.jpg");
}



Configuration does not limit mime type

Main code

app.UseStaticFiles(new StaticFileOptions()
{
    
    
	//不限制content-type下载
	ServeUnknownFileTypes = true,

	配置的虚拟路径映射
	//RequestPath = "/local",
	物理地址
	//FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider("D:\\Work\\西南油气田图库系统\\WebNetCore5_Img_Storage\\WebNetCore5_Img_Storage\\bin\\Debug\\net5.0"),
});
      // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
    
    
            if (env.IsDevelopment())
            {
    
    
                app.UseDeveloperExceptionPage();
            }
            else
            {
    
    
                app.UseExceptionHandler("/Error");
            }
            app.UseHttpsRedirection();
                  
            app.UseStaticFiles(new StaticFileOptions()
            {
    
    
                //不限制content-type下载
                ServeUnknownFileTypes = true,

                配置的虚拟路径映射
                //RequestPath = "/local",
                物理地址
                //FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider("D:\\Work\\西南油气田图库系统\\WebNetCore5_Img_Storage\\WebNetCore5_Img_Storage\\bin\\Debug\\net5.0"),
            });

            app.UseRouting();

            //app.UseAuthentication();
            app.UseAuthorization();

            app.UseSession();

            //app.UseResponseCaching();
            //app.UseResponseCompression();

            //用MVC模式, 针对services的services.AddControllersWithViews();
            app.UseEndpoints(endpoints =>
            {
    
    
                //endpoints.MapDefaultControllerRoute();
                endpoints.MapRazorPages();

                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

        }

Reprint

.NetCore download file
https://www.cnblogs.com/wangrudong003/p/7592689.html

The "virtual directory" setting in Asp.Net Core
https://www.cnblogs.com/EminemJK/p/13362368.html

Guess you like

Origin blog.csdn.net/u011511086/article/details/113592552