Ciao a tutti sto usando un updatepanel per caricare l' immagine del profilo degli utenti. Voglio controllare che l' immagine esista, abbia una certa estensione e che non superi una dimensione massima. Stranamente quando faccio partire la funzione lato server non riesco a leggere il contenuto del fileupload, che sembra vuoto.
codice:
<div id="popup"> <asp:UpdatePanel ID="updpnl" runat="server" UpdateMode="Conditional"> <ContentTemplate> <asp:Label ID="lbl1" runat="server" Text="Scegli un' immagine per il tuo profilo"></asp:Label> <asp:FileUpload ID="fup1" runat="server" /> <asp:Button ID="btnok" runat="server" Text="Salva" onclick="btnok_Click" /> <asp:Label ID="lblerrore" runat="server" Text="" ForeColor="Red"></asp:Label> <asp:Label ID="lblok" runat="server" Text=""></asp:Label> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="btnok" EventName="Click" /> </Triggers> </asp:UpdatePanel> <asp:UpdateProgress AssociatedUpdatePanelID="updpnl" runat="server" DisplayAfter="0"> <ProgressTemplate> Attendere... </ProgressTemplate> </asp:UpdateProgress> </div>
Questa è la funzione:
codice:
protected void btnok_Click(object sender, EventArgs e) { string ext=System.IO.Path.GetExtension(fup1.FileName); if (fup1.HasFile == false) { lblerrore.Text = "Scegli un file"; } else { if (((ext == ".jpg") || (ext == ".gif") || (ext == ".bmp")) == false) { lblerrore.Text = "Puoi caricare file .jpg , .gif , .bmp"; } else { if (fup1.PostedFile.ContentLength > 3000000) { lblerrore.Text = "L' immagine è troppo grande"; } else { MySqlConnection conn = new MySqlConnection(************); MySqlCommand comm = new MySqlCommand("UPDATE my_dati SET immagine=@img WHERE nome=@nome;", conn); conn.Open(); HttpPostedFile pos=null; pos=fup1.PostedFile; byte[] file=new byte[pos.ContentLength]; pos.InputStream.Read(file,0,pos.ContentLength); comm.Parameters.AddWithValue("nome", Session["nome"].ToString()); comm.Parameters.AddWithValue("img", file); if (comm.ExecuteNonQuery()==1) { lblok.Text = "Immagine salvata"; } else { lblerrore.Text = "Errore nel salvataggio dell' immagine"; } conn.Close(); } } }
Quando provo a caricare un file i appare "Scegli un file", come se non lo avessi fatto. Perchè?