skip to Main Content

It is working on the same tab though.

<a href="path" class="some-class" target="_blank">Terms of Use</a>

It is working for the file types .html and .txt but not for .pdf even .docx files get downloaded.
I am writing this code in cshtml.

I want to open a pdf document in a new tab.

2

Answers


  1. Here’s an example of what I do for this…

    In view (.cshtml):

    <a asp-page="/OpenFile" target="_blank" asp-route-file-id="@Model.Quotes.QuotedPart.PDF-File-ID"><img src="/images/PDF_file_icon.svg" width="30" height="30" /></a>
               
    

    In controller (OpenFile.cs):

      // lookup file's stored path from DB: this becomes "filename" which in my case includes folder/filename
        var memory = new MemoryStream();
                    string baseDir = AppContext.BaseDirectory;
                    string fullPath = baseDir + filename;
                    
         using (var stream = new FileStream(fullPath, FileMode.Open))
                        {
                            await stream.CopyToAsync(memory);
                        }
                        memory.Position = 0;
                   
                        return File(memory, "application/pdf");
                      
    

    The key seemed to be to return File object with "application/pdf" mimetype without the 3rd parameter string of filename.

    Login or Signup to reply.
  2. Any local downloaded file should work with "_blank" if user has not set their browser to download only (rather than download after view).

    So simply point as you have done, to the a href="local.pdf" or if its remote the URL.pdf

    enter image description here

    However

    that same file in some recent chromium’s like Edge may be truly a blob blank
    ! (so in the run up to adding Acrobat as replacing Skia/Foxit), MS ? or chromium? seem to have lost the plot?

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search