6

Never done this before so not sure what is involved in it. I did search and found many answers but they were more complex than what I need. For example they needed to zoom in, generate, create accurate thumbnail, embed the actual PDF in the webpage, etc... But my question is much simpler: If my guy that I am showing his information on webpage has some PDF to show I just want to put a generic PDF icon on the page, people click on it and the actual PDF opens in a new tab in their browser.

What is involved in just doing that? It is not like a file path, the PDF is saved in SQL server as binary objects or whatever it does to save it in SQL Server..it is not a file disk path on server

Bohn
  • 26,091
  • 61
  • 167
  • 254
  • Is the PDF embedded or downloaded from server? You question in its current form is too broad to get a good answer. – Nkosi May 26 '16 at 15:06
  • @Nkosi I query the guy's data like name, last name, etc..from server. There is also an Images table there than has the JPEG, PDF, etc.. linked to primary key of the guy so I query that. – Bohn May 26 '16 at 15:08
  • Check @Fran 's answer – Nkosi May 26 '16 at 15:09
  • @Nkosi the PDF is saved in SQL server as binary objects or whatever it does to save it in SQL Server..it is not a file disk path on server – Bohn May 26 '16 at 15:12
  • Ok then you need to expose an `action` (mvc or webapi) that will receive the request for the PDF, extract it from database and return it to client. On the client you do like in the answer given to call it. – Nkosi May 26 '16 at 15:13
  • @Nkosi Can't we do just some sort of memory stream reader ? and then somehow download it locally to client's computer maybe and then open it from there in new tab? – Bohn May 26 '16 at 15:14
  • 2
    @Bohn: That's not really how the Internet works. The web server just returns a response, which consists of headers (which among other things, tells the client, or browser, the mime-type of the response body) and the response body itself (here, the raw PDF data). It will naturally stream, in a sense, as TCP/IP is packet based, so the PDF data comes down in chunks and the client stitches it back together. However, once it hits the client's machine, you have no control over anything anymore. Technically, once the response begins being returned you no longer have control. – Chris Pratt May 26 '16 at 15:40
  • 1
    The target attribute on the link itself is what hints to the browser to open a new tab. – Chris Pratt May 26 '16 at 15:41
  • @ChrisPratt Thanks for explanations. – Bohn May 26 '16 at 15:42

2 Answers2

9

Your tags indicate asp.net-mvc.

Create a controller to handle requests for the PDF file

Pseudo:

[RoutePrefix("Pdf")]
public class PdfController : Controller {
    [Route("{id}"]
    public ActionResult GetPDF(int id) {    
        //...Code to extract pdf from SQLServer and store in stream
        Stream stream = GetDataFromSQLServerById(id);
        return File(stream,"filename.pdf");
    }
}

On client

<a href="/Pdf/123456" target="_blank">
    <img src="images/pdficon.jpg">
</a>

Update:

Referencing @ChrisPratt's comment; (which I forgot to include in my answer)

The target attribute on the anchor tag is what will tell the browser to open the link in a new tab.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Thanks, gave me the general idea. But what are RoutePrefix("Pdf")] and [Route("{id}"] and do I need them? Also do I need a new controller like you have here? or I can just pretty much have that GetDataFromSQLServerById in my model ? – Bohn May 26 '16 at 15:31
  • 1
    I was using attribute routing. I was just showing an example. You can add that action to a controller of your own choosing. – Nkosi May 26 '16 at 15:34
  • That's for attribute routing. You don't need them unless you're using attribute routing. Otherwise, just make them what you want the URL to be. – Chris Pratt May 26 '16 at 15:34
  • I used this solution for my project and it worked perfectly, only my visual studio now tells me that the target attribute is only allowed with a href and I am currently using asp-action. Any ideas on whether I should change my code or ignore the warning? – Rob Rombouts Dec 10 '18 at 14:07
  • @RobRombouts show a brief example of the markup. I haven't completely grasped what you are referring to. – Nkosi Dec 10 '18 at 14:13
  • I am using this code to display an icon – Rob Rombouts Dec 11 '18 at 15:06
  • 1
    @RobRombouts ok, based on that I can say that you can ignore that warning. The tag will have the href once rendered. – Nkosi Dec 11 '18 at 15:27
1

Create a Controller action to for your link

public PdfResult GetPdf(int databaseRecordId)
{
    var dbRecord = your code to return the sql record.

    return new PdfResult(dbRecord.PdfBytes, "whatever name you want to show.pdf");

}

public class PdfResult : FileResult
{
    private const String DefaultFileName = "file.pdf";
    private readonly Byte[] _byteArray;

    public PdfResult(Byte[] byteArray, String fileName = DefaultFileName)
        : base(MediaTypeNames.Application.Pdf)
    {
        _byteArray = byteArray;
        FileDownloadName = fileName;
    }
    protected override void WriteFile(HttpResponseBase response) { response.BinaryWrite(_byteArray); }
}

view code

<a href="<controller/action/databaseRecordId>" target="_blank">
<img src="<image-path>">
</a>
arpit rai
  • 21
  • 1
  • 7
Fran
  • 6,440
  • 1
  • 23
  • 35
  • yeah. i didn't want to remove that one because the question changed from how to open a pdf in a new tab to open a pdf byte array from the db into a new tab – Fran May 26 '16 at 15:35