Files not saved if you have response.redirect in page_load

Jan 24, 2010 at 5:22 PM

This is a strange problem that I'm having... the fileuploader control will not save any files if I have "response.redirect" in my page_load method.

I have narrowed it down to one line of code in my page_load that causes fileuploader to not save the file.

Here is my Page_Load:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim ProfileName As String
            ProfileName = Request.QueryString("username")

            If ProfileName = "" Then
            End If

        End If
End Sub

If I comment out that one line with response.redirect in it, the files get saved as expected.  The problem is, I need that line in my code... anybody have any ideas?

Feb 18, 2010 at 11:43 AM
Edited Feb 18, 2010 at 12:21 PM

Hi, I was having similar issues and traced the problem back to the fact that the fileUpload doesn't do a full postback, instead the page loads as a new request without any QueryString/PostBack data. In your example above the page is loading without the username QueryString parameter and is redirecting the request before it reaches the file saving code.

Initially I fixed the issue by using the redirect overload to stop the reponse from ending after the redirect:

Response.Redirect("~/home", false)

But this isn't the most ideal solution really since you don't want the Not IsPostBack code to run at all. After digging around a bit that there is a 'FileIsPosted' property that lets you know if it is a file upload request, use this in your page load like the IsPostBack property. Link to another post about this issue:

This is a bit annoying really because it makes the upload control a pain to use in custom controls, where the parent page won't know about the FileIsPosted property. This is a problem in my scenario. There is a RequestAsPostBack property on the control, but it doesn't seem to do what I expect. When I set the flag to true and upload a file, it doesn't trigger either the Page_Load event or my FileReceived event.

Anyway, I hope thats enough info for you.

- Joel