-2

I am inherited a vb.net website and am trying to add a page that allows downloading .pdf files from the server.

This was my original attempt as doing this: Clickable Link Not appearing on page from generated anchor tag and it failed because I am generating a anchor tag in javascript with the runat="server".
I now understand why that does not work.

So, my question has changed to: If I have a simple anchor tag like this:

<a href='download.aspx?type=inspection&file=NNNNNN.pdf'>Download NNNNNN.pdf</a>`

What does download.aspx and download.aspx.vb look like?

Do I need to change anything in IIS to make it aware that is a valid URL?

I am asking this question on how to do this without asp controls because the examples I find on the web all rely on asp controls.

I am a Windows/IIS/vb.net noob so please be as specific as you can in the answer.

This question Reading a binary file and using Response.BinaryWrite() does not answer my question because it is code fragments and not entire files. I am too new to vb.net to be confident to put those fragments together into a file and debug it.

Be Kind To New Users
  • 9,672
  • 13
  • 78
  • 125
  • What exactly isn't working? It's not clear to me where you're stuck. All you show here is a link to a URL. Have you implemented the page at that URL? Is something failing? – David Sep 11 '17 at 18:02
  • Take a look at https://stackoverflow.com/q/1300729/656243 – Lynn Crumbling Sep 11 '17 at 18:03
  • Where is the PDF file stored on the server? In a database, or on the web server's file system? Is the PDF file a 'static' document (i.e. completely under control of the webmaster), or is something that can be uploaded by web users? – Ruud Helderman Sep 11 '17 at 18:32
  • Possible duplicate of [Reading a binary file and using Response.BinaryWrite()](https://stackoverflow.com/questions/848679/reading-a-binary-file-and-using-response-binarywrite) – Ruud Helderman Sep 11 '17 at 18:41

1 Answers1

2

What does download.aspx ... look like?

This could be the entirety of the download.aspx file:

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="download.vb" %>

What does ... download.vb look like?

Something like this:

Imports System;
Imports System.Web;

Public Partial Class Download Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object sender, e As EventArgs)
        Response.Clear()
        Response.ClearHeaders()
        Response.ClearContent()

        Response.AddHeader("Content-Disposition", "attachment; filename=YourFileNameHere")
        Response.ContentType = "content type here (ie: application/x-pdf)";

        'Load and write the file data to the Response stream here

        Response.End();    
    End Sub
End Class

You can also get a (small) boost to performance by using a an HttpHandler (*.ashx) instead of a Page.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Here is the list of what I did differently from the answer I accepted: a) Change "Class Download" to "Class MY_PATH_Download", b) Change "CodeFile="download.vb"" to "CodeFile="Download.aspx.vb" Inherits="MY_PATH_Download"" – Be Kind To New Users Sep 11 '17 at 22:00