asp.net實(shí)現(xiàn)網(wǎng)站上傳下載功能的幾種方法-創(chuàng)新互聯(lián)

能實(shí)現(xiàn)網(wǎng)站下載的功能語(yǔ)言都是動(dòng)態(tài)語(yǔ)言,除了asp.net,還有php,jsp等,這里以asp.net為例。

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比澧縣網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式澧縣網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋澧縣地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。

 這種可以實(shí)現(xiàn)多種網(wǎng)站下載功能, 代碼如下:

//TransmitFile實(shí)現(xiàn)下載
   protected void Button1_Click(object sender, EventArgs e)
    {

        Response.ContentType = "application/x-zip-compressed";
        Response.AddHeader("Content-Disposition", "p_w_upload;filename=z.zip");
       string filename = Server.MapPath("DownLoad/aaa.zip");
        Response.TransmitFile(filename);
    }

   //WriteFile實(shí)現(xiàn)下載
   protected void Button2_Click(object sender, EventArgs e)
    {

string fileName ="aaa.zip";//客戶端保存的文件名
       string filePath=Server.MapPath("DownLoad/aaa.zip");//路徑

        FileInfo fileInfo = new FileInfo(filePath);
        Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
        Response.AddHeader("Content-Disposition", "p_w_upload;filename=" + fileName);
        Response.AddHeader("Content-Length", fileInfo.Length.ToString());
        Response.AddHeader("Content-Transfer-Encoding", "binary");
        Response.ContentType = "application/octet-stream";
        Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        Response.WriteFile(fileInfo.FullName);
        Response.Flush();
        Response.End();

//WriteFile分塊下載
   protected void Button3_Click(object sender, EventArgs e)
    {

       string fileName = "aaa.zip";//客戶端保存的文件名
       string filePath = Server.MapPath("DownLoad/aaa.zip");//路徑

        System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

       if (fileInfo.Exists == true)
        {
           const long ChunkSize = 102400;//100K 每次讀取文件,只讀取100K,這樣可以緩解服務(wù)器的壓力
           byte[] buffer = new byte[ChunkSize];

            Response.Clear();
            System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
           long dataLengthToRead = iStream.Length;//獲取下載的文件總大小
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Disposition", "p_w_upload; filename=" + HttpUtility.UrlEncode(fileName));
           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();
        }
    }

   //流方式下載
   protected void Button4_Click(object sender, EventArgs e)
    {
       string fileName = "aaa.zip";//客戶端保存的文件名
       string filePath = Server.MapPath("DownLoad/aaa.zip");//路徑

       //以字符流的形式下載文件
        FileStream fs = new FileStream(filePath, FileMode.Open);
       byte[] bytes = new byte[(int)fs.Length];
        fs.Read(bytes, 0, bytes.Length);
        fs.Close();
        Response.ContentType = "application/octet-stream";
       //通知瀏覽器下載文件而不是打開

Response.AddHeader("Content-Disposition", "p_w_upload;  filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();

    }

完畢(end)

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

文章題目:asp.net實(shí)現(xiàn)網(wǎng)站上傳下載功能的幾種方法-創(chuàng)新互聯(lián)
鏈接URL:http://muchs.cn/article2/pssic.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、自適應(yīng)網(wǎng)站網(wǎng)站設(shè)計(jì)公司、品牌網(wǎng)站建設(shè)外貿(mào)建站、標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)