当前位置: 移动技术网 > IT编程>开发语言>.net > 更有效的文件下载功能

更有效的文件下载功能

2019年04月19日  | 移动技术网IT编程  | 我要评论

寄书长不达,肖秀丹,大虎头蜂

1.普通下载:


        //下载文件的路径
        string path=server.mappath("广告.jpg");
        //下载文件的名称
        string filename = "广告.jpg";
        system.io.fileinfo todownload = new system.io.fileinfo(path);
        response.clear();
        if (system.io.path.getextension(filename) == ".jpg")
        ...{
            response.addheader("content-disposition", "attachment;filename=new_" + httputility.urlencode(todownload.name));
            response.contenttype = "application/x-zip-compressed";
            response.transmitfile(path);
            response.end();
        }
 

2.大文件分成小块下载:


        //下载文件的路径
        string path = server.mappath("广告.jpg");
        //下载文件的名称
        string filename = "广告.jpg";
        system.io.fileinfo todownload = new system.io.fileinfo(path);

        if (todownload.exists == true)
        {
            const long chunksize = 10000;
            byte[] buffer = new byte[chunksize];

            response.clear();
            system.io.filestream istream = system.io.file.openread(path);
            long datalengthtoread = istream.length;
            response.contenttype = "application/octet-stream";
            response.addheader("content-disposition", "attachment; filename=new_" + httputility.urlencode(todownload.name));
            while (datalengthtoread > 0 && response.isclientconnected)
            {
                int lengthread = istream.read(buffer, 0, convert.toint32(chunksize));
                response.outputstream.write(buffer, 0, lengthread);
                response.flush();
                datalengthtoread = datalengthtoread - lengthread;
            }
            response.close();
        }

 

 


摘自 爱智旮旯

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网