ASP.NET Coreはファイルをダウンロードし、画像をダウンロードするようにブラウザーに通知します

画像をダウンロードするようにブラウザに通知するには、主にサーバー側で応答ヘッダーを設定します

ここに画像の説明を挿入します

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

ASP.NETCoreダウンロードファイル;画像のダウンロード



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



構成はmimeタイプを制限しません

メインコード

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

        }

転載

.NetCoreダウンロードファイル
https://www.cnblogs.com/wangrudong003/p/7592689.html

Asp.NetCoreの「仮想ディレクトリ」設定
https://www.cnblogs.com/EminemJK/p/13362368.html

おすすめ

転載: blog.csdn.net/u011511086/article/details/113592552