|
|
IN-VESTI DNW!!! Sono finalmente arrivate le nuovissime T-Shirt di DotNetWork!!! Con soli 15,00 € ci sosterrai nelle spese di gestione della Community e ti invieremo a casa una splendida maglietta. Se vuoi contribuire al mantenimento di DotNetWork.it Vai sulla pagina Iscrizioni Effettua il pagamento usando IWBank Oppure un Bonifico bancario (le coordinate sono sulla pagina Iscrizioni), inviaci una mail a support@dotnetwork.it indicandoci la tua taglia e l'indirizzo di spedizione. Non appena verificata la ricezione del pagamento provvederemo a spedirti la tua T-Shirt. Le magliette sono disponibili nelle taglie S-M-L-XL-XXL (in caso di esaurimento di una delle taglie, indica quella di "Backup"). Grazie per IN-VESTIRTI con NOI!!! .:DotNetWork Founders:.
|
|
|
|
|
[VB 2008] Download di file
Ultimo Post 30 ott 2009 15.49 by Giovanni. 6 Risposte.
|
Ordina:
|
| Autore |
Messaggi |
 |
 Posts:54
 |
| 23 ott 2009 21.41 |
|
Ciao a tutti. Ho un gridView che ha una colonna ButtonField. I dati arrivano da una lista di oggetti di una classe da me creata. Una volta aggiunti i dati al gridView nell'evento RowCommand ho aggiunto questo codice:
If e.CommandName = "DownLoad" Then Dim nomefile As String = restituisciValoreColonna(e, gvFileIntestatari, 1) Dim basePath As String = Server.MapPath("~/DownLoad/") Dim fullPath As String = Path.Combine(basePath, nomefile) EseguiAzioneDownLoad(nomefile) File.Delete(fullPath) End If
Il codice di EseguiAzioneDownload contiene a sua volta la seguente sub:
Sub Page_Loader(ByVal nomeFile As String) Response.Expires = -1 Dim basePath As String = Server.MapPath("~/DownLoad/") Dim fullPath As String = Path.Combine(basePath, nomeFile) Dim fileEs As FileInfo = New FileInfo(fullPath) ' verifica esistenza If fileEs.Exists Then ' imposta le headers Response.Clear() Response.AddHeader("Content-Disposition", "attachment; filename=" & fileEs.Name) Response.AddHeader("Content-Length", fileEs.Length.ToString()) Response.ContentType = "application/octet-stream" ' leggo dal file e scrivo nello stream di risposta Response.WriteFile(fullPath) Response.End() Else Response.Write("Impossibile scaricare il file.") End If End Sub
Fin qui tutto bene, se non che la mia intenzione è quella di eliminare il file dalla cartella DownLoad dopo averlo scaricato. Per questo motivo ho aggiunto l'istruzione
File.Delete(fullPath)
ma non funziona. Ho visto che non viene proprio letta. Sembra che dopo aver scaricato il file tutto il rimanente codice non è letto. Avevo anche provato a mettere un messaggio di avviso dell'imminente eliminazione del file, ma ho raddoppiato il problema, ovvero, la finestra di avviso appare due volte ed il file non è eliminato. Qualcuno sa dirmi perché e suggerirmi l'eventuale soluzione? Grazie. Giovanni Puglisi. |
|
|
|
|
 Posts:11
 |
| 26 ott 2009 12.41 |
|
Ciao Giovanni.
Il tuo problema è qui : Response.WriteFile(fullPath) Response.End()
nel momento che chiami il response.end() fermi l'esecuzione del codice e quindi la tua istruzione di delete non viene eseguita...
io ti suggerirei di spostare il delete del file all'interno del metodo Page_Loader.
If fileEs.Exists Then Dim _oBlob As Byte() = System.IO.File.ReadAllBytes(fullPath) System.IO.File.Delete(fullPath) Response.AddHeader("Content-Disposition", "attachment;filename=" + nomeFile) Response.ContentType = "application/octet-stream" Response.AddHeader("Content-length", _oBlob.Length.ToString) Response.AddHeader("Content-Transfer-Encoding", "binary") Response.BinaryWrite(_oBlob) Response.End() end if
Il codice mi sembra leggibile, se non ti è chiara qualcosa fammi sapere.
Fabio.
|
|
|
|
|
 Posts:54
 |
| 27 ott 2009 08.40 |
|
Grazie Fabio. Ho provato la tua soluzione e funziona. Ma rimane il problema che il codice si esegue due volte (e non capisco il perché); così succede che la prima volta legge ed elimina il file, la seconda non lo trova più e quindi genera l'errore. Sicuramente sbaglio qualcosa nel gestire l'evento RowCommand del gridView. Ma cosa? |
|
|
|
|
 Posts:11
 |
| 27 ott 2009 11.39 |
|
Dovrei vedere tutto il codice per risponderti. Se puoi postarlo...
|
|
|
|
|
 Posts:54
 |
| 27 ott 2009 13.33 |
|
Ecco il codice della pagina : Sub Page_Loader(ByVal nomeFile As String) Response.Expires = -1 Dim basePath As String = Server.MapPath("~/DownLoad/") Dim fullPath As String = Path.Combine(basePath, nomeFile) Dim fileEs As FileInfo = New FileInfo(fullPath) ' verifica esistenza If fileEs.Exists Then Dim _oBlob As Byte() = System.IO.File.ReadAllBytes(fullPath) System.IO.File.Delete(fullPath) Response.AddHeader("Content-Disposition", "attachment;filename=" + nomeFile) Response.ContentType = "application/octet-stream" Response.AddHeader("Content-length", _oBlob.Length.ToString) Response.AddHeader("Content-Transfer-Encoding", "binary") Response.BinaryWrite(_oBlob) Response.End() Else Response.Write("Impossibile scaricare il file.") End If End Sub
' Con questa Sub metto i dati che mi necessitano nella gridView Private Sub PopolaGridViewFile() Dim listaFile As New List(Of FileIntestatari) Dim elencoFiles() As String = Nothing Dim nomefile As String = String.Empty Dim basePath As String = Server.MapPath("~/DownLoad/") Dim idIndagine As Integer = 0 If gvIndagini.SelectedIndex -1 Then idIndagine = AcquisisciIdIndagine() End If Dim id As String = Convert.ToString(idIndagine) elencoFiles = Directory.GetFiles(basePath) If elencoFiles IsNot Nothing Then For Each el As String In elencoFiles nomefile = Path.GetFileName(el) If nomefile.StartsWith(id) Then Dim fInfo As New FileInfo(el) Dim dimensione As Integer = fInfo.Length Dim dataDiCreazione As Date = fInfo.CreationTime Dim FileIntest As New FileIntestatari With FileIntest .Nome = nomefile .DataDiCreazione = dataDiCreazione .Dimensione = CType(dimensione, String) & " byte" End With listaFile.Add(FileIntest) End If Next End If gvFileIntestatari.DataSource = listaFile gvFileIntestatari.DataBind() End Sub
Private Function AcquisisciIdIndagine() As Integer Dim idIndagine As Integer Me.lblErrorMessage.Text = "" If gvIndagini.SelectedIndex -1 Then Dim row As GridViewRow = gvIndagini.SelectedRow idIndagine = CType(row.Cells(1).Text, Integer) Else Me.lblErrorMessage.Text = "Attenzione! Non hai selezionato un'indagine dall'elenco." Me.lblErrorMessage.ForeColor = Drawing.Color.Red Exit Function End If Return idIndagine End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'Carico la gridView con i dati esistenti PopolaGridViewFile() End Sub
Private Sub EseguiAzioneDownLoad(ByVal nomefile As String) Dim basePath As String = Server.MapPath("~/DownLoad/") Dim fullPath As String = Path.Combine(basePath, nomeFile) Page_Loader(nomefile) End Sub
Function restituisciValoreColonna(ByVal e As CommandEventArgs, ByVal gv As GridView, ByVal colonna As Integer) As String 'metodo per RowCommand Dim index As Integer = Convert.ToInt32(e.CommandArgument) Dim row As GridViewRow = gv.Rows(index) Dim value As String = String.Empty value = row.Cells(colonna).Text Return value End Function
Function restituisciValoreColonna(ByVal e As System.EventArgs, ByVal gv As GridView, ByVal colonna As Integer) As String 'metodo per SelectedIndexChanged Dim index As Integer = gv.SelectedIndex Dim row As GridViewRow = gv.Rows(index) Dim value As String = String.Empty value = row.Cells(colonna).Text Return value End Function
Protected Sub gvFileIntestatari_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvFileIntestatari.RowCommand If e.CommandName = "DownLoad" Then Dim nomefile As String = restituisciValoreColonna(e, gvFileIntestatari, 1) Dim basePath As String = Server.MapPath("~/DownLoad/") Dim fullPath As String = Path.Combine(basePath, nomefile) EseguiAzioneDownLoad(nomefile) End If End Sub
Protected Sub gvIndagini_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvIndagini.SelectedIndexChanged PopolaGridViewFile() End Sub
Grazie. Giovanni Puglisi.
|
|
|
|
|
 Posts:11
 |
| 27 ott 2009 17.40 |
|
Ciao Giovanni,
ho dato un occhiata al volo al codice, a naso il problema è qui:
Protected Sub gvIndagini_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles gvIndagini.SelectedIndexChanged PopolaGridViewFile() End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 'Carico la gridView con i dati esistenti PopolaGridViewFile() End Sub
Nel senso che in caso di post back dovuto all'evento "gvIndagini.SelectedIndexChanged" tu esegui la sub " PopolaGridViewFile() " due volte. Nel page load della pagina e poi nell'handler dell'evento.
Prova a modificare la page_load cosi : Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load IF NOT Page.IsPostBack then
PopolaGridViewFile() END IF
End Sub
|
|
|
|
|
 Posts:54
 |
| 30 ott 2009 15.49 |
|
Finalmente ho risolto, e me ne vergogno anche un pò. Avevo inavvertitamente duplicato il nome di uno dei due gridview presenti nella pagina e da qui la duplicazione del codice. Che fatica! Grazie tante Fabio. A presto. Giovanni Puglisi |
|
|
|
|
| Non sei autorizzato ad inviare una risposta. |
|
Active Forums 4.1
|
|
|
|
|
|
|
|
|
.NET e l'Interoperabilità COM |
 |
Come utilizzare Dll non referenziabili per convertire un documento Word in PDF |
 |
|
2008/07/05 | Autore: Massimo Lofrano
|
|
|
|
|
|
|
Briciole di ereditarietà |
 |
Come aggiungere ulteriori funzionalità alla TreeView personalizzata |
 |
|
2009/06/05 | Autore: Francesca Mazzoni
|
|
|
|
|
|
|
|