要通知浏览器下载图片,主要是在服务器端设置响应头
Content-Disposition: attachment; filename=20210201160550_567_d9a7.jpg; filename*=UTF-8''20210201160550_567_d9a7.jpg
ASP.NET Core下载文件;下载图片
//虚拟路径下载 图片
[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.Net Core 中的“虚拟目录”设置
https://www.cnblogs.com/EminemJK/p/13362368.html