Telecharger un fichier en ASP.NEt
Solidseb
-
fxz -
fxz -
Bonjour,
Voila je tente de télécharger un fichier csv que j'ai cré précedement sur un serveur pour pouvoir le sauvegarder sur un pc y ayant accés , j'utilise donc ce code :
Dim iStream As System.IO.Stream
' Buffer to read 10K bytes in chunk:
Dim buffer(10000) As Byte
' Length of the file:
Dim length As Integer
' Total bytes to read:
Dim dataToRead As Long
' Identify the file to download including its path.
Dim filepath As String = "C:\Stats.csv"
' Identify the file name.
Dim filename As String = System.IO.Path.GetFileName(filepath)
Try
' Open the file.
iStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open)
' Total bytes to read:
dataToRead = iStream.Length
Response.ContentType = "application/octet-stream"
'Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment; filename=" & filename)
Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0")
Response.AddHeader("Pragma", "no-cache")
Response.AddHeader("Expires", 0)
' Read the bytes.
While dataToRead > 0
' Verify that the client is connected.
If Response.IsClientConnected Then
' Read the data in buffer
length = iStream.Read(buffer, 0, 10000)
'Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length)
' Flush the data to the HTML output.
Response.Flush()
ReDim buffer(10000) ' Clear the buffer
dataToRead = dataToRead - length
Else
'prevent infinite loop if user disconnects
dataToRead = -1
End If
End While
Catch ex As Exception
' Trap the error, if any.
Response.Write("Error : " & ex.Message)
Finally
If IsNothing(iStream) = False Then
' Close the file.
iStream.Close()
' Dim objFSO = CreateObject("Scripting.FileSystemObject")
' objFSO.DeleteFile("C:\Stats.csv")
' Microsoft.VisualBasic.SetAttr("C:\Stats.csv", FileAttribute.Normal)
IO.File.Delete("C:\Stats.csv")
End If
End Try
Et en effet le téléchargement fonctionne bien sauf qu'en plus du contenu de mon fichier il me rajoute le code aspx de la page ... Quelqu'un saurait t'il comment réglé ce probleme ?
Voila je tente de télécharger un fichier csv que j'ai cré précedement sur un serveur pour pouvoir le sauvegarder sur un pc y ayant accés , j'utilise donc ce code :
Dim iStream As System.IO.Stream
' Buffer to read 10K bytes in chunk:
Dim buffer(10000) As Byte
' Length of the file:
Dim length As Integer
' Total bytes to read:
Dim dataToRead As Long
' Identify the file to download including its path.
Dim filepath As String = "C:\Stats.csv"
' Identify the file name.
Dim filename As String = System.IO.Path.GetFileName(filepath)
Try
' Open the file.
iStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open)
' Total bytes to read:
dataToRead = iStream.Length
Response.ContentType = "application/octet-stream"
'Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader("Content-Disposition", "attachment; filename=" & filename)
Response.AddHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0")
Response.AddHeader("Pragma", "no-cache")
Response.AddHeader("Expires", 0)
' Read the bytes.
While dataToRead > 0
' Verify that the client is connected.
If Response.IsClientConnected Then
' Read the data in buffer
length = iStream.Read(buffer, 0, 10000)
'Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length)
' Flush the data to the HTML output.
Response.Flush()
ReDim buffer(10000) ' Clear the buffer
dataToRead = dataToRead - length
Else
'prevent infinite loop if user disconnects
dataToRead = -1
End If
End While
Catch ex As Exception
' Trap the error, if any.
Response.Write("Error : " & ex.Message)
Finally
If IsNothing(iStream) = False Then
' Close the file.
iStream.Close()
' Dim objFSO = CreateObject("Scripting.FileSystemObject")
' objFSO.DeleteFile("C:\Stats.csv")
' Microsoft.VisualBasic.SetAttr("C:\Stats.csv", FileAttribute.Normal)
IO.File.Delete("C:\Stats.csv")
End If
End Try
Et en effet le téléchargement fonctionne bien sauf qu'en plus du contenu de mon fichier il me rajoute le code aspx de la page ... Quelqu'un saurait t'il comment réglé ce probleme ?
A voir également:
- Telecharger un fichier en ASP.NEt
- Fichier bin - Guide
- Télécharger clavier arabe - Télécharger - Divers Web & Internet
- Comment réduire la taille d'un fichier - Guide
- Comment ouvrir un fichier epub ? - Guide
- Fichier rar - Guide
Va dans le code HTML de la page , et supprime tout à part la directive @PAGE
Easy, isnt it ?