system.io.stream istream = null;
// buffer to read 10k bytes in chunk:
byte[] buffer = new byte[10000];
// length of the file:
int length;
// total bytes to read:
long datatoread;
// identify the file to download including its path.
string filepath = "downloadfilename";
// identify the file name.
string filename = system.io.path.getfilename(filepath);
try
{
// open the file.
istream = new system.io.filestream(filepath, system.io.filemode.open,
system.io.fileaccess.read,system.io.fileshare.read);
// total bytes to read:
datatoread = istream.length;
response.contenttype = "application/octet-stream";
response.addheader("content-disposition", "attachment; filename=" + filename);
// read the bytes.
while (datatoread > 0)
{
// verify that the client is connected.
if (response.isclientconnected)
{
// 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();
buffer= new byte[10000];
datatoread = datatoread - length;
}
else
{
//prevent infinite loop if user disconnects
datatoread = -1;
}
}
}
catch (exception ex)
{
// trap the error, if any.
response.write("error : " + ex.message);
}
finally
{
if (istream != null)
{
//close the file.
istream.close();
}
}
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器