problem with checking FileUpload

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bart

    #1

    problem with checking FileUpload

    Hi,

    i use the FileUpload control but if the uploaded file already exists, i want
    let the choice between overwriting or not the existing file.
    Therefore i use a radiobuttonlist with 'yes' and 'no'.

    I get no error, but if i take 'yes', the new file which overwrites the
    existing one is empty. I think it's because of the postabck caused by the
    radiobuttonlist , so the Fileupload control looses the chosen file. I put the
    FileUpLoad1.Fil eName in a hiddenfield in order to get it back after the 2nd
    postback, but no succes.

    Thanks for help
    Bart

    Here my code:
    aspx:
    <asp:FileUpLo ad id="FileUpLoad1 " runat="server" /><br /><br />
    <asp:Button id="Button1" Text="Opladen" runat="server" /><br /><br
    />
    <asp:Label id="Label1" runat="server" />
    <asp:HiddenFiel d ID="HiddenField 1" runat="server" />
    <asp:RadioButto nList ID="RadioButton List1" runat="server"
    Visible=false AutoPostBack=tr ue>
    <asp:ListItem Value="ja" Text="ja" ></asp:ListItem>
    <asp:ListItem Value="nee" Text="nee" ></asp:ListItem>
    </asp:RadioButton List>

    code-behind:

    Protected Sub Button1_Click1( ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    If FileUpLoad1.Has File Then
    If File.Exists(Ser ver.MapPath("~/uploaddir/" &
    FileUpLoad1.Fil eName)) Then
    HiddenField1.Va lue = FileUpLoad1.Fil eName
    Label1.Text = FileUpLoad1.Fil eName & " is already on server.
    Overwrite?"
    RadioButtonList 1.Visible = True
    Else
    Label1.Visible = False
    FileUpLoad1.Sav eAs(Server.MapP ath("~/uploaddir/" &
    FileUpLoad1.Fil eName))
    End If
    End If
    End Sub

    Protected Sub RadioButtonList 1_SelectedIndex Changed(ByVal sender As
    Object, ByVal e As System.EventArg s) Handles
    RadioButtonList 1.SelectedIndex Changed
    If RadioButtonList 1.SelectedValue = "yes" Then
    Label1.Visible = False
    FileUpLoad1.Sav eAs(Server.MapP ath("~/uploaddir/" &
    HiddenField1.Va lue))
    Else
    RadioButtonList 1.Visible = False
    Label1.Visible = False
    End If
    End Sub
    End Class



  • John Timney \(MVP\)

    #2
    Re: problem with checking FileUpload

    You should save the file on the server first as a tempoirary file, and then
    present the information that the file already exists. If yes, replace the
    existing file - if no delete the temp one.

    Regards

    John Timney (MVP)




    "Bart" <b@swrote in message
    news:%23A5jOsUo HHA.1000@TK2MSF TNGP05.phx.gbl. ..
    Hi,
    >
    i use the FileUpload control but if the uploaded file already exists, i
    want let the choice between overwriting or not the existing file.
    Therefore i use a radiobuttonlist with 'yes' and 'no'.
    >
    I get no error, but if i take 'yes', the new file which overwrites the
    existing one is empty. I think it's because of the postabck caused by the
    radiobuttonlist , so the Fileupload control looses the chosen file. I put
    the FileUpLoad1.Fil eName in a hiddenfield in order to get it back after
    the 2nd postback, but no succes.
    >
    Thanks for help
    Bart
    >
    Here my code:
    aspx:
    <asp:FileUpLo ad id="FileUpLoad1 " runat="server" /><br /><br />
    <asp:Button id="Button1" Text="Opladen" runat="server" /><br /><br
    />
    <asp:Label id="Label1" runat="server" />
    <asp:HiddenFiel d ID="HiddenField 1" runat="server" />
    <asp:RadioButto nList ID="RadioButton List1" runat="server"
    Visible=false AutoPostBack=tr ue>
    <asp:ListItem Value="ja" Text="ja" ></asp:ListItem>
    <asp:ListItem Value="nee" Text="nee" ></asp:ListItem>
    </asp:RadioButton List>
    >
    code-behind:
    >
    Protected Sub Button1_Click1( ByVal sender As Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    If FileUpLoad1.Has File Then
    If File.Exists(Ser ver.MapPath("~/uploaddir/" &
    FileUpLoad1.Fil eName)) Then
    HiddenField1.Va lue = FileUpLoad1.Fil eName
    Label1.Text = FileUpLoad1.Fil eName & " is already on
    server. Overwrite?"
    RadioButtonList 1.Visible = True
    Else
    Label1.Visible = False
    FileUpLoad1.Sav eAs(Server.MapP ath("~/uploaddir/" &
    FileUpLoad1.Fil eName))
    End If
    End If
    End Sub
    >
    Protected Sub RadioButtonList 1_SelectedIndex Changed(ByVal sender As
    Object, ByVal e As System.EventArg s) Handles
    RadioButtonList 1.SelectedIndex Changed
    If RadioButtonList 1.SelectedValue = "yes" Then
    Label1.Visible = False
    FileUpLoad1.Sav eAs(Server.MapP ath("~/uploaddir/" &
    HiddenField1.Va lue))
    Else
    RadioButtonList 1.Visible = False
    Label1.Visible = False
    End If
    End Sub
    End Class
    >
    >
    >

    Comment

    • Bart

      #3
      Re: problem with checking FileUpload

      Thanks


      "John Timney (MVP)" <x_john@timney. eclipse.co.uksc hreef in bericht
      news:MpmdnaKJ2b tu1sbbnZ2dnUVZ8 vWdnZ2d@eclipse .net.uk...
      You should save the file on the server first as a tempoirary file, and
      then present the information that the file already exists. If yes, replace
      the existing file - if no delete the temp one.
      >
      Regards
      >
      John Timney (MVP)


      >
      >
      "Bart" <b@swrote in message
      news:%23A5jOsUo HHA.1000@TK2MSF TNGP05.phx.gbl. ..
      >Hi,
      >>
      >i use the FileUpload control but if the uploaded file already exists, i
      >want let the choice between overwriting or not the existing file.
      >Therefore i use a radiobuttonlist with 'yes' and 'no'.
      >>
      >I get no error, but if i take 'yes', the new file which overwrites the
      >existing one is empty. I think it's because of the postabck caused by the
      >radiobuttonlis t, so the Fileupload control looses the chosen file. I put
      >the FileUpLoad1.Fil eName in a hiddenfield in order to get it back after
      >the 2nd postback, but no succes.
      >>
      >Thanks for help
      >Bart
      >>
      >Here my code:
      >aspx:
      ><asp:FileUpLoa d id="FileUpLoad1 " runat="server" /><br /><br />
      > <asp:Button id="Button1" Text="Opladen" runat="server" /><br /><br
      >/>
      > <asp:Label id="Label1" runat="server" />
      > <asp:HiddenFiel d ID="HiddenField 1" runat="server" />
      > <asp:RadioButto nList ID="RadioButton List1" runat="server"
      >Visible=fals e AutoPostBack=tr ue>
      > <asp:ListItem Value="ja" Text="ja" ></asp:ListItem>
      > <asp:ListItem Value="nee" Text="nee" ></asp:ListItem>
      > </asp:RadioButton List>
      >>
      >code-behind:
      >>
      >Protected Sub Button1_Click1( ByVal sender As Object, ByVal e As
      >System.EventAr gs) Handles Button1.Click
      > If FileUpLoad1.Has File Then
      > If File.Exists(Ser ver.MapPath("~/uploaddir/" &
      >FileUpLoad1.Fi leName)) Then
      > HiddenField1.Va lue = FileUpLoad1.Fil eName
      > Label1.Text = FileUpLoad1.Fil eName & " is already on
      >server. Overwrite?"
      > RadioButtonList 1.Visible = True
      > Else
      > Label1.Visible = False
      > FileUpLoad1.Sav eAs(Server.MapP ath("~/uploaddir/" &
      >FileUpLoad1.Fi leName))
      > End If
      > End If
      > End Sub
      >>
      > Protected Sub RadioButtonList 1_SelectedIndex Changed(ByVal sender As
      >Object, ByVal e As System.EventArg s) Handles
      >RadioButtonLis t1.SelectedInde xChanged
      > If RadioButtonList 1.SelectedValue = "yes" Then
      > Label1.Visible = False
      > FileUpLoad1.Sav eAs(Server.MapP ath("~/uploaddir/" &
      >HiddenField1.V alue))
      > Else
      > RadioButtonList 1.Visible = False
      > Label1.Visible = False
      > End If
      > End Sub
      >End Class
      >>
      >>
      >>
      >
      >

      Comment

      Working...