Why paypal Redirect is executed after Page_Load?

守給你的承諾、 提交于 2019-12-24 22:14:35

问题


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.

  1. 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.

  2. Redirect to a page that is set to auto-refresh and downloads when auto-refreshed. This bypasses the .NET limitation that using Response.TransmitFile() and Response.End() makes it impossible to then use Response.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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!