问题
Oh dear paypal,
I'd like to know why the "return redirect Url" page won't load if I call Response.AppendHeader
. You see, I'm trying to make a file download automatically after the user has payed on paypal's page so I put the following code on my Page_Load
If Not IsNothing(Request.QueryString("paymentId")) Then
Dim paypalUtils As New PayPalUtils
paypalUtils.ProcessPayPalResponse(Request.QueryString("paymentId").ToString(), Request.QueryString("PayerID").ToString())
TransmitFile()
End If
My TransmitFile
implementation is very simple
Protected Sub TransmitFile()
Response.ContentType = "application/sla"
Response.AppendHeader("Content-Disposition", "attachment; filename=" & Path.GetFileNameWithoutExtension(hidFileName.Value) & ".stl")
Response.TransmitFile(Sistema.SavePath & Path.GetFileNameWithoutExtension(hidFileName.Value) & ".stl")
Response.End()
End Sub
The problem is that somehow calling Response.AppendHeader
or any of the following lines prevents paypal from correctly redirecting to the proper url that I specified on the API.
If I comment the TransmitFile
on Page_Load everything works as expected (Paypal redirects me to my site). If not, I sucessfully make the file download automatically but I get stuck on this page

What is that I'm doing wrong?
回答1:
Your code is actually correct in how the download is being delivered - it's just how .NET handles the file transmission, redirection, and page display that's causing the confusion.
You have a few options at your disposal, depending on how you want the experience to be on your site.
Use AJAX and iFrames to asynchronously download the file when your redirect page is displayed. See this StackOverflow question for more answers. The link to a walkthrough on Encosia is very detailed and should make for a nice user experience.
Redirect to a page that is set to auto-refresh and downloads when auto-refreshed. This bypasses the .NET limitation that using
Response.TransmitFile()
andResponse.End()
makes it impossible to then useResponse.Redirect()
. To do the auto-refresh, you add the following line to the<head>
portion of your HTML source code:<meta http-equiv="refresh" content="1; url=<%= this.RedirectUrl %>">
this.RedirectUrl
references a property in the code behind the page that is set to wherever you want the redirect to go. After that redirect is handled is when you run your code to send the file.
I was able to test out #2 and got it working. I'm sure there's more possibilities as well, but hopefully that should get things working for you.
来源:https://stackoverflow.com/questions/28952584/why-paypal-redirect-is-executed-after-page-load