How to return a file that open in browser embed not download?

mc 4,516 Reputation points
2022-08-23T03:34:10.043+00:00

I am using .net core web api and in a api there is return file (content-type:application/octet-stream)

but the browser will start download not open it.

in the html there is a form post to the web api.

I want to the file will open (pdf) not download

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,573 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. JasonPan - MSFT 5,796 Reputation points Microsoft Vendor
    2022-08-23T14:08:07.937+00:00

    Hi @mc

    You can use FileStreamResult to show pdf file, it will not start download.

    234123-202201.gif

    You can change your code like below, and create a wwwroot folder in your project.

    Program.cs

    namespace SwaggerApi  
    {  
        public class Program  
        {  
            public static void Main(string[] args)  
            {  
                var builder = WebApplication.CreateBuilder(args);  
      
                // Add services to the container.  
                // Add this line for show pdf  
                builder.Services.AddHttpClient("PdfDomain", client => {  
                    client.BaseAddress = new Uri("https://localhost:7055/");  
                });  
                builder.Services.AddControllers();  
                // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle  
                builder.Services.AddEndpointsApiExplorer();  
                builder.Services.AddSwaggerGen();  
      
                var app = builder.Build();  
      
                // Configure the HTTP request pipeline.  
                if (app.Environment.IsDevelopment())  
                {  
                    app.UseSwagger();  
                    app.UseSwaggerUI();  
                }  
                app.UseSwagger();  
                app.UseSwaggerUI();  
      
                app.UseHttpsRedirection();  
                // add this for access static files under wwwroot folder  
                app.UseStaticFiles();  
                  
                app.UseAuthorization();  
      
                app.MapControllers();  
      
                app.Run();  
            }  
        }  
    }  
    

    PdfController.cs

    using Microsoft.AspNetCore.Mvc;  
      
    namespace SwaggerApi.Controllers  
    {  
        [ApiController]  
        [Route("[controller]")]  
        public class PdfController : ControllerBase  
        {  
            private readonly IHttpClientFactory _httpClientFactory;  
            public PdfController(IHttpClientFactory httpClientFactory)  
            {  
                _httpClientFactory = httpClientFactory;  
            }  
            [HttpGet]  
            [Route("external-domain-pdf")]  
            public async Task<IActionResult> ExternlDomainPdf()  
            {  
                string url = "/images/sample.pdf";  
                var httpClient = _httpClientFactory.CreateClient("PdfDomain");  
                var response = await httpClient.GetAsync(url);  
                MemoryStream ms = new MemoryStream(await response.Content.ReadAsByteArrayAsync());  
                return new FileStreamResult(ms, "application/pdf");  
            }  
        }  
    }  
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Jason

    0 comments No comments

  2. Bruce (SqlWork.com) 65,576 Reputation points
    2022-08-23T16:25:07.76+00:00

    the Content-Disposition header control how the browser handle the download "inline" means open attachment means save.

    // download  
    Content-Disposition: attachment; filename=sample.pdf;  
      
    // open  
    Content-Disposition: inline; filename="sample.pdf"  
      
    

    to know its a pdf file Content-Type should be application/pdf

    so change your headers to:

    Content-Type: application/pdf  
    Content-Disposition: inline; filename="sample.pdf"  
      
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.