Dispaly PDF and also update the form control

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi, Experts,

I have a problem that I could not solve and hope someone could shed some light on it. Here is what I need to do:

on my page, there is textbox that user can enter an orderID, a "Get USPS label" button user can click, another textbox that will contain USPS tracking number. I need to implement it that if user click on the "Get USPS label" button, a pdf of the usps label will come up, and the tracking number will be populated on the textbox.

In the Button_Click server-side code, I use the orderID to get information and call USPS API to get the label xml that contains tracking number and pdf encoded as base64 string. then I use the following code to output the pdf to user and assign the tracking number back to a textbox control:

Response.AddHeader("content-type", "application/pdf");
Resposne.AddHeader("content-disposition", "attachment; file=uspslabel.pdf");
byte[] pdfdata = Convert.FromBase64String(sPDF);
Response.BinaryWrite(pdfdata);
Response.Flush();

this.txtTrackingNumber.Text = "TrackingNumberFromUSPS";

The pdf will come back but the tracking number assignment never shows on the page. How can I solve this problem? Do I have to use javascript?

Thanks a lot!
John
 
The pdf will come back but the tracking number assignment never shows on
the page. How can I solve this problem? Do I have to use javascript?

Yes, you will have to use javascript. HTTP allows only only response per
request, and thus you cannot send both a PDF file and a HTML file at once.

I believe the best approach here is to let the form post result in the
PDF-response, and then use the client-side submit event to update the
contents of the HTML page, if it is still available. This might not be the
case if the user agent decides to display the PDF-document in the browser
window.
 
anyone has code snippet for this scenario?

Joern Schou-Rode said:
Yes, you will have to use javascript. HTTP allows only only response per
request, and thus you cannot send both a PDF file and a HTML file at once.

I believe the best approach here is to let the form post result in the
PDF-response, and then use the client-side submit event to update the
contents of the HTML page, if it is still available. This might not be the
case if the user agent decides to display the PDF-document in the browser
window.
 
Hi John,

My name is Allen Chen. It's my pleasure to work with you on this issue.

First, Joern is correct that HTTP allows only one response per request so
we cannot send the pdf file and html content in one response.

To solve this problem, a straightforward solution is to send two requests.
For example, we can use window.open to send a get request to the page to
download the file and use the normal post request to update the TextBox
that is sent after clicking the button.

Here's the code snippet that can achieve the requirement:

Aspx:

<asp:Button ID="Button1" runat="server" Text="Download"
onclick="Button1_Click"
OnClientClick="window.open('Default.aspx?downloadfile=true')"/>

<asp:TextBox ID="TextBox1" runat="server" >
</asp:TextBox>
Aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString.Count > 0 &&
Request.QueryString["downloadfile"] == "true")
{
//download the file
Response.AddHeader("content-type", "application/pdf");
Resposne.AddHeader("content-disposition", "attachment; file=uspslabel.pdf");
byte[] pdfdata = Convert.FromBase64String(sPDF);
Response.BinaryWrite(pdfdata);
Response.End();
}
}


protected void Button1_Click(object sender, EventArgs e)
{
//Update TextBox
this.TextBox1.Text = "TrackingNumberFromUSPS";

}

Please have a try and let me know if it works. If you have further
questions please feel free to ask.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Thanks Allen!

The approach is cool but it seems not help on my issue.

Since each time I retrieve a USPS label, the tracking number will be
different so I can only retrieve once in my code behind code, there I have
both tracking number and base64 string that represents the pdf.

your approach would allow tracking number assigned to the textbox, but use
window.open() to open a new window with the same page or different page -
how do I pass the pdf string to that new page? I cannot pass it through url
since it's big, I cannot put it into session object since we have non-sticky
server farm.

Thanks!
John


Allen Chen said:
Hi John,

My name is Allen Chen. It's my pleasure to work with you on this issue.

First, Joern is correct that HTTP allows only one response per request so
we cannot send the pdf file and html content in one response.

To solve this problem, a straightforward solution is to send two requests.
For example, we can use window.open to send a get request to the page to
download the file and use the normal post request to update the TextBox
that is sent after clicking the button.

Here's the code snippet that can achieve the requirement:

Aspx:

<asp:Button ID="Button1" runat="server" Text="Download"
onclick="Button1_Click"
OnClientClick="window.open('Default.aspx?downloadfile=true')"/>

<asp:TextBox ID="TextBox1" runat="server" >
</asp:TextBox>
Aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString.Count > 0 &&
Request.QueryString["downloadfile"] == "true")
{
//download the file
Response.AddHeader("content-type", "application/pdf");
Resposne.AddHeader("content-disposition", "attachment;
file=uspslabel.pdf");
byte[] pdfdata = Convert.FromBase64String(sPDF);
Response.BinaryWrite(pdfdata);
Response.End();
}
}


protected void Button1_Click(object sender, EventArgs e)
{
//Update TextBox
this.TextBox1.Text = "TrackingNumberFromUSPS";

}

Please have a try and let me know if it works. If you have further
questions please feel free to ask.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support
Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
Allow me to jump in since I had worked with labels.
Usually it's smart to keep the labels in some kind of storage anyway for
reprinting purposes.
Then it becomes easy to output PDF just to print the label by passing
tracking number in Url string.

PS: Do you mind telling me which provider do you use to generate USPS
labels? I had used endicia.com but they are expensive for first class
labels.

Thanks
George.


John said:
Thanks Allen!

The approach is cool but it seems not help on my issue.

Since each time I retrieve a USPS label, the tracking number will be
different so I can only retrieve once in my code behind code, there I have
both tracking number and base64 string that represents the pdf.

your approach would allow tracking number assigned to the textbox, but use
window.open() to open a new window with the same page or different page -
how do I pass the pdf string to that new page? I cannot pass it through
url since it's big, I cannot put it into session object since we have
non-sticky server farm.

Thanks!
John


Allen Chen said:
Hi John,

My name is Allen Chen. It's my pleasure to work with you on this issue.

First, Joern is correct that HTTP allows only one response per request
so
we cannot send the pdf file and html content in one response.

To solve this problem, a straightforward solution is to send two
requests.
For example, we can use window.open to send a get request to the page to
download the file and use the normal post request to update the TextBox
that is sent after clicking the button.

Here's the code snippet that can achieve the requirement:

Aspx:

<asp:Button ID="Button1" runat="server" Text="Download"
onclick="Button1_Click"
OnClientClick="window.open('Default.aspx?downloadfile=true')"/>

<asp:TextBox ID="TextBox1" runat="server" >
</asp:TextBox>
Aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString.Count > 0 &&
Request.QueryString["downloadfile"] == "true")
{
//download the file
Response.AddHeader("content-type", "application/pdf");
Resposne.AddHeader("content-disposition", "attachment;
file=uspslabel.pdf");
byte[] pdfdata = Convert.FromBase64String(sPDF);
Response.BinaryWrite(pdfdata);
Response.End();
}
}


protected void Button1_Click(object sender, EventArgs e)
{
//Update TextBox
this.TextBox1.Text = "TrackingNumberFromUSPS";

}

Please have a try and let me know if it works. If you have further
questions please feel free to ask.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you.
Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each
follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of
this
nature are best handled working with a dedicated Microsoft Support
Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.
 
Hi John,

Thanks for your update.

I think you can pass the order ID via querystring. Then use the USPS API to
retrieve the pdf. Like below:

Aspx:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function Download()
{
var orderid=document.getElementById("orderID_TextBox").value;
window.open('Default.aspx?orderid=' + orderid);
}
</script>
</head>
<body>
<form id="form1" runat="server">
Order ID:
<asp:TextBox ID="orderID_TextBox" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Download"
onclick="Button1_Click"
OnClientClick="Download()"/>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

</form>
</body>
</html>

Aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{

if (Request.QueryString["orderid"] !=null)
{ //Get order id via querystring:
//string orderid = Request.QueryString["orderid"];
//Call USPS API to retrieve pdf and download it to client side
}
}

protected void Button1_Click(object sender, EventArgs e)
{ //Get order id via orderID_TextBox.Text
//string orderid= orderID_TextBox.Text;
//Call USPS API to retrieve tracking number and update TextBox
this.TextBox1.Text = "TrackingNumberFromUSPS";

}

From your description, however, it seems the USPS API could only send back
the tracking number and the pdf content as a whole, right? If so I don't
think the above code is very efficient because it calls that API twice and
there's no need to get the pdf in the call to get the tracking number.

I didn't use that API before so I'm not sure whether there's an API that
only returns the tracking number. If there's I believe the above solution
is good enough.

Please have a try and let me know if you have further questions.

Regards,
Allen Chen
Microsoft Online Support
 
Hi John,

Do you have any progress on this issue?

Regards,
Allen Chen
Microsoft Online Support
 
Back
Top