• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

Asp.net Change File Name at Runtime?


I have a page a that you select a company on -> click Submit -> this takes you to the report.aspx page.  
The report.aspx page has over 300 lines of data in a table.  The table is highly stylized.

What I need to do is either make a PDF with the company name as the filename OR
Produce an .aspx page with the company name as the filename and the user can print it to PDF.  

To create a pdf I've tried using
<%Response.AddHeader("content-disposition", "Attachment;Filename=" & thisCompanyName & ".pdf")
    Response.ContentType = "application/pdf"%>
The file doesn't generate

I've looked at iTextSharp and it looks like it will take a bunch of coding to create the table with all the stylings in it.

To produce a new file
I took the report.aspx and change the page title to the company name then print it physically to pdf - the file comes out with Report.aspx file name.  Tab at the top of the browser displays the correct company name.  (Thought I used this before) No good.

Any ideas?

  • 5
  • 2
1 Solution
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim url = "http://www.htm2pdf.co.uk/convert_url.php"
        Using client = New Net.WebClient
            Dim data As New Specialized.NameValueCollection
            Dim targetUrl = ""http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28669582.html"
            data.Add("url", targetUrl)
            client.Proxy = Nothing
            Dim bytes = client.UploadValues(url, data)
            Dim response = ""
            Using stm As New IO.MemoryStream(bytes)
                Using reader As New IO.StreamReader(stm)
                    response = reader.ReadToEnd
                End Using
            End Using
            Dim regex = New System.Text.RegularExpressions.Regex("(file=[^>]*)>")
            Dim fileUrl = "http://www.htm2pdf.co.uk/download.php?" & regex.Match(response).Groups(1).Value
            client.DownloadFile(fileUrl, "c:\temp.pdf")
        End Using
    End Sub

Open in new window

here is a windowsapp sample which use html2pdf service. fileUrl is an address to download pdf file. In your web app you can simply add a link with this url. You can do this either from server side using WebClient/WebHTTPRequest or from client side via xmlhttprequest or ajax.
jshesekAuthor Commented:
The data can't be uploaded anywhere.
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

jshesekAuthor Commented:
Is there anyway you can use add header and change the filename when the page is created?  
If the page is called report.aspx -> can I change it to AcmeCo.aspx in the coding?
jshesekAuthor Commented:
Is it possible for this to happen?
Start out on SelectCompany.aspx    select the company from a drop down
This sends you to report.aspx - Report.aspx is loaded with the data
Can I Copy and rename the report.aspx to newCompanyName.aspx   and transfer to it?
jshesekAuthor Commented:
Ok solved the problem with

Generate the Report.aspx page
 File.Copy(Server.MapPath("") + "\\Report.aspx", (Server.MapPath("") + "\\" & thisReportName & ".aspx"))
File.Copy(Server.MapPath("") + "\\Report.aspx.vb", (Server.MapPath("") + "\\" & thisReportName & ".aspx.vb"))

Closed the report.aspx page and set a link on the select the company page.
jshesekAuthor Commented:
Solved the problem
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now