By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,016 Members | 2,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,016 IT Pros & Developers. It's quick & easy.

problem with checking FileUpload

P: n/a
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.FileName 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:FileUpLoad id="FileUpLoad1" runat="server" /><br /><br />
<asp:Button id="Button1" Text="Opladen" runat="server" /><br /><br
/>
<asp:Label id="Label1" runat="server" />
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
Visible=false AutoPostBack=true>
<asp:ListItem Value="ja" Text="ja" ></asp:ListItem>
<asp:ListItem Value="nee" Text="nee" ></asp:ListItem>
</asp:RadioButtonList>

code-behind:

Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
If FileUpLoad1.HasFile Then
If File.Exists(Server.MapPath("~/uploaddir/" &
FileUpLoad1.FileName)) Then
HiddenField1.Value = FileUpLoad1.FileName
Label1.Text = FileUpLoad1.FileName & " is already on server.
Overwrite?"
RadioButtonList1.Visible = True
Else
Label1.Visible = False
FileUpLoad1.SaveAs(Server.MapPath("~/uploaddir/" &
FileUpLoad1.FileName))
End If
End If
End Sub

Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
RadioButtonList1.SelectedIndexChanged
If RadioButtonList1.SelectedValue = "yes" Then
Label1.Visible = False
FileUpLoad1.SaveAs(Server.MapPath("~/uploaddir/" &
HiddenField1.Value))
Else
RadioButtonList1.Visible = False
Label1.Visible = False
End If
End Sub
End Class

May 28 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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)
http://www.johntimney.com
http://www.johntimney.com/blog
"Bart" <b@swrote in message
news:%2****************@TK2MSFTNGP05.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.FileName 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:FileUpLoad id="FileUpLoad1" runat="server" /><br /><br />
<asp:Button id="Button1" Text="Opladen" runat="server" /><br /><br
/>
<asp:Label id="Label1" runat="server" />
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
Visible=false AutoPostBack=true>
<asp:ListItem Value="ja" Text="ja" ></asp:ListItem>
<asp:ListItem Value="nee" Text="nee" ></asp:ListItem>
</asp:RadioButtonList>

code-behind:

Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
If FileUpLoad1.HasFile Then
If File.Exists(Server.MapPath("~/uploaddir/" &
FileUpLoad1.FileName)) Then
HiddenField1.Value = FileUpLoad1.FileName
Label1.Text = FileUpLoad1.FileName & " is already on
server. Overwrite?"
RadioButtonList1.Visible = True
Else
Label1.Visible = False
FileUpLoad1.SaveAs(Server.MapPath("~/uploaddir/" &
FileUpLoad1.FileName))
End If
End If
End Sub

Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
RadioButtonList1.SelectedIndexChanged
If RadioButtonList1.SelectedValue = "yes" Then
Label1.Visible = False
FileUpLoad1.SaveAs(Server.MapPath("~/uploaddir/" &
HiddenField1.Value))
Else
RadioButtonList1.Visible = False
Label1.Visible = False
End If
End Sub
End Class

May 28 '07 #2

P: n/a
Thanks
"John Timney (MVP)" <x_****@timney.eclipse.co.ukschreef in bericht
news:Mp******************************@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)
http://www.johntimney.com
http://www.johntimney.com/blog
"Bart" <b@swrote in message
news:%2****************@TK2MSFTNGP05.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.FileName 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:FileUpLoad id="FileUpLoad1" runat="server" /><br /><br />
<asp:Button id="Button1" Text="Opladen" runat="server" /><br /><br
/>
<asp:Label id="Label1" runat="server" />
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:RadioButtonList ID="RadioButtonList1" runat="server"
Visible=false AutoPostBack=true>
<asp:ListItem Value="ja" Text="ja" ></asp:ListItem>
<asp:ListItem Value="nee" Text="nee" ></asp:ListItem>
</asp:RadioButtonList>

code-behind:

Protected Sub Button1_Click1(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
If FileUpLoad1.HasFile Then
If File.Exists(Server.MapPath("~/uploaddir/" &
FileUpLoad1.FileName)) Then
HiddenField1.Value = FileUpLoad1.FileName
Label1.Text = FileUpLoad1.FileName & " is already on
server. Overwrite?"
RadioButtonList1.Visible = True
Else
Label1.Visible = False
FileUpLoad1.SaveAs(Server.MapPath("~/uploaddir/" &
FileUpLoad1.FileName))
End If
End If
End Sub

Protected Sub RadioButtonList1_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
RadioButtonList1.SelectedIndexChanged
If RadioButtonList1.SelectedValue = "yes" Then
Label1.Visible = False
FileUpLoad1.SaveAs(Server.MapPath("~/uploaddir/" &
HiddenField1.Value))
Else
RadioButtonList1.Visible = False
Label1.Visible = False
End If
End Sub
End Class


May 28 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.