ASP.NET Core descarga el archivo e informa al navegador que descargue la imagen

Para notificar al navegador que descargue la imagen, lo principal es configurar el encabezado de respuesta en el lado del servidor

Inserte la descripción de la imagen aquí

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

ASP.NET Core descargar archivos; descargar imágenes



//虚拟路径下载 图片
[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");
}



La configuración no limita el tipo de mimo

Código principal

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?}");
            });

        }

Reimprimir

Archivo de descarga .NetCore
https://www.cnblogs.com/wangrudong003/p/7592689.html

La configuración del "directorio virtual" en Asp.Net Core
https://www.cnblogs.com/EminemJK/p/13362368.html

Supongo que te gusta

Origin blog.csdn.net/u011511086/article/details/113592552
Recomendado
Clasificación