Bonjour,
j'ai remarqué une différence concernant la manière dont IIS 7 traitait System.IO.Stream par rapport à IIS 6.
On peut facilement reproduire ce comportement en utilisant un IIS6 d'un coté et un IIS7 de l'autre et le code suivant :
default.aspx
Code Snippet
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
script runat="server">
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
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 = ConfigurationManager.AppSettings("Fichier")
' 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, IO.FileAccess.Read, IO.FileShare.Read)
' Total bytes to read:
dataToRead = iStream.Length
Response.ContentType =
"application/octet-stream"
Response.AddHeader(
"Content-Disposition", "attachment; filename=" & filename)
Dim smtp As New SmtpClient
smtp.Send(ConfigurationManager.AppSettings(
"Expediteur"), ConfigurationManager.AppSettings("Destinataire"), "D‚but du t‚l‚chargement", "le t‚l‚chargement … commenc‚ … " & Now.ToShortTimeString)
' 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
smtp.Send(ConfigurationManager.AppSettings(
"Expediteur"), ConfigurationManager.AppSettings("Destinataire"), "Fin du t‚l‚chargement", "le t‚l‚chargement s'est termin‚ … " & Now.ToShortTimeString)
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()
End If
End Try
End Sub
</< FONT>
script>
<
html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
<title>Page sans titre</< FONT>title>
</< FONT>
head>
<
body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Download Now !" OnClick="Button1_Click" />
</< FONT>div>
</< FONT>form>
</< FONT>
body>
</< FONT>
html>
Web.config
Code Snippet
<?
xml version="1.0" encoding="utf-8"?>
<
configuration>
<
appSettings>
<
add key="Expediteur" value="test@test.com" />
<
add key="Destinataire" value="test@test.com" />
<
add key="Fichier" value="c:\Fichier.zip" />
</
appSettings>
<
system.net>
<
mailSettings>
<
smtp from="test@test.com">
<
network host="baobab" password="" userName="" />
</
smtp>
</
mailSettings>
</
system.net>
</
configuration>
ce code peut aussi etre téléchargé ici : http://web.ngri.fr/testDL.zip
il convient juste de bien configurer web.config pour que cela fonctionne.
Le comportement attendu est celui de IIS 6 :
1- On clique pour télécharger un fichier et une boite de téléchargement du fichier apparait. Un premier email signalant le début du download est envoyé.
2- le téléchargement a lieu
3- Le téléchargement est terminé, un email indiquant la fin du download est envoyé.
Avec IIS 7, on reçoit l'email de fin avant la fin du téléchargement.
Ce comportement nous pose un grave problème sur une appli.
Merci d'avance