欢迎光临
我们一直在努力

Asp.net(c#)实现多线程断点续传-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

 

system.io.stream istream = null;

            // buffer to read 10k bytes in chunk:
            byte[] buffer = new byte[10240];

            // length of the file:
            int length;

            // total bytes to read:
            long datatoread;

            // identify the file to download including its path.
            string filepath  = @”e:\software\sql server 2000 personal edition.iso”;

            // 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);
                response.clear();

                // total bytes to read:
                datatoread = istream.length;

                long p = 0;
                if(request.headers[“range”]!=null)
                {
                    response.statuscode = 206;
                    p = long.parse( request.headers[“range”].replace(“bytes=”,””).replace(“-“,””));
                }
                if(p != 0)
                {
                    response.addheader(“content-range”,”bytes ” + p.tostring() + “-” + ((long)(datatoread – 1)).tostring() + “/” + datatoread.tostring());                   
                }
                response.addheader(“content-length”,((long)(datatoread-p)).tostring());
                response.contenttype = “application/octet-stream”;
                response.addheader(“content-disposition”, “attachment; filename=” + system.web.httputility.urlencode(request.contentencoding.getbytes(filename)));

                istream.position = p;
                datatoread = datatoread – p;
                // 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, 10240);

                        // 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[10240];
                        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();
                }
                   response.end();
            }

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Asp.net(c#)实现多线程断点续传-.NET教程,Asp.Net开发
分享到: 更多 (0)