国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

Asp.net(c#)實現多線程斷點續傳

2024-07-10 13:12:03
字體:
來源:轉載
供稿:網友

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();
            }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宝坻区| 诸城市| 绥化市| 镇远县| 云和县| 垣曲县| 岳阳县| 乌拉特后旗| 泽州县| 武穴市| 陵水| 分宜县| 广安市| 张家界市| 红原县| 丹江口市| 余庆县| 梁山县| 黑河市| 定襄县| 大埔县| 遂昌县| 宜城市| 山丹县| 盱眙县| 若羌县| 娱乐| 岫岩| 靖远县| 维西| 杭锦旗| 邢台县| 永平县| 平武县| 巫山县| 鄂伦春自治旗| 辽阳市| 阿拉善左旗| 宜宾县| 三穗县| 博罗县|