Torna al Thread
Public Sub UploadFoto(ByVal sender As Object, ByVal e As System.EventArgs)
Try
'+++ anche se potrei usare direttamente SENDER, lo associo ad un oggetto dichiarato come AsyncFileUpload
'cosi posso sfruttare l'intellisense +++
'MsgBox(sender.postedfile.contenttype)
'MsgBox(AsyncFileUploadControl.PostedFile.FileName)
Dim AsyncFileUploadControl As AjaxControlToolkit.AsyncFileUpload
AsyncFileUploadControl = sender
Dim ContenitorePanel As Panel = AsyncFileUploadControl.Parent
Dim ContenitoreRepItem As RepeaterItem = ContenitorePanel.Parent
Dim ImgUploadControl As Image = RepeaterChiSiamo.Items(ContenitoreRepItem.ItemIndex).FindControl("imgUpload")
Dim lblUploadDescriptionControl As Label = RepeaterChiSiamo.Items(ContenitoreRepItem.ItemIndex).FindControl("lblUploadDescription")
Dim lblNomeFileCaricatoControl As Label = RepeaterChiSiamo.Items(ContenitoreRepItem.ItemIndex).FindControl("lblNomeFileCaricato")
'+++ verifica del tipo di file caricato... accetto solo files immagine
Dim fileType As String = ""
fileType = Strings.Left(AsyncFileUploadControl.PostedFile.ContentType, 6)
If fileType <> "image/" Then
MsgBox("Tipo di file non permesso...Puoi caricare solamente files di tipo IMMAGINE!")
fileType = ""
Exit Sub
End If
'+++ se arrivo qui, verifica dimensioni file... +++
Dim section As System.Web.Configuration.HttpRuntimeSection
section = ConfigurationManager.GetSection("system.web/httpRuntime")
Dim MaxLenghtFilePermitted As Integer
MaxLenghtFilePermitted = section.MaxRequestLength
'+++ verifico se il file è vuoto...+++
If AsyncFileUploadControl.PostedFile.ContentLength <= 0 Then
MsgBox("file vuoto !")
Exit Sub
End If
If (AsyncFileUploadControl.PostedFile.ContentLength / 1024) > MaxLenghtFilePermitted Then
MsgBox("file troppo grande!")
Exit Sub
End If
'Step3. Receive the file from the AsyncFileUpload web control and then assign it to fileuploadreceive1 variable
Dim fileuploadreceiveFoto As String = AsyncFileUploadControl.PostedFile.FileName
'Step4. Extract the filename from the uploaded file.
Dim filenameFoto As String = System.IO.Path.GetFileName(fileuploadreceiveFoto)
'Step5. Define the path where the file will be saved in your server.
Dim fileuploadpathFoto As String = HttpContext.Current.Request.MapPath(ImgPath & "Gestori")
'Step6. Save the file.
AsyncFileUploadControl.PostedFile.SaveAs(System.IO.Path.Combine(fileuploadpathFoto, filenameFoto))
'+++ caricamento istantaneo immagine ... +++
ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "UploadFotoNuovo", "window.parent.document.getElementById('" + ImgUploadControl.ClientID + "').src='" + ImgPath + "Gestori/" + filenameFoto + "';", True)
'+++ definizioni caratteristiche file appena caricato
Dim decDimensioni As Decimal
Dim strdimensioni As String = ""
decDimensioni = CDec((AsyncFileUploadControl.PostedFile.ContentLength / 1024))
strdimensioni = String.Format("{0:N}", decDimensioni)
ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "DescrizioneAlert", "window.parent.document.getElementById('" + lblUploadDescriptionControl.ClientID + "').innerHTML ='" + Resources.ResourcePanel.strTipoDiFile + " " + AsyncFileUploadControl.PostedFile.ContentType + "<br>" + Resources.ResourcePanel.strDimensioniInKB + " " + strdimensioni + "';", True)
decDimensioni = 0.0
strdimensioni = ""
'+++ inserimento del nome file caricato in una label per successivo recupero e salvataggio sul db +++
ScriptManager.RegisterClientScriptBlock(Me, GetType(Page), "NomeFileCaricato", "window.parent.document.getElementById('" + lblNomeFileCaricatoControl.ClientID + "').innerHTML ='" + filenameFoto + "';", True)
If IsNothing(AsyncFileUploadControl) = False Then AsyncFileUploadControl.Dispose()
If IsNothing(ImgUploadControl) = False Then ImgUploadControl.Dispose()
If IsNothing(lblNomeFileCaricatoControl) = False Then lblNomeFileCaricatoControl.Dispose()
If IsNothing(lblUploadDescriptionControl) = False Then lblUploadDescriptionControl.Dispose()
Catch ex As Exception
Dim clsGen As New ClassGenerali
If ViewMsgDebug = True Then clsGen.MostraMessaggio(HttpContext.Current.Handler, Resources.ResourceMsg.Errore, ex.Message)
'SEND MAIL CON ERRORE ESTESO
Call clsGen.ScriviLog("Panel/ChiSiamo.aspx.vb_UploadFoto", ex.Message)
clsGen.SendMailProblemi(ex.Message & "<br><br>Errore in fase di caricamento foto edit record ChiSiamo")
clsGen = Nothing
Finally
End Try
End Sub