zoukankan      html  css  js  c++  java
  • asp.net 下载文件源代码(转)

     .net获取 网站的虚拟目录 名字 

    Request.ApplicationPath.TrimStart('/')


    增加命名空间: 
    using  System.Net;  
    using  System.IO;  

    /// <summary> 
      /// 下载文件 
      /// </summary> 
      /// <param name="URL">要下载文件网址</param> 
      public void downloadfile(string URL) 
      { 
       WebClient client=new WebClient(); 
       int n  =  URL.LastIndexOf('/');  
       string URLAddress  =  URL.Substring(0,n);  //取得网址 
       string fileName  =  URL.Substring(n+1,URL.Length-n-1);  //取得文件名 
       string Dir  = Server.MapPath("./");  //下载文件存放路径 
       
       string Path  =  Dir+'\\'+fileName; //下载文件存放完整路径 
        
       Stream stream  =  client.OpenRead(URL);  
        
       StreamReader reader  =  new  StreamReader(stream);  
       byte[] mbyte  =  new  byte[100000];  
       int allbyte  =  (int)mbyte.Length;  
       int startbyte  =  0;      
       while(allbyte>0)  //循环读取 
       {  
        int  m  =  stream.Read(mbyte,startbyte,allbyte);  
        if(m==0)  
         break;  
          
        startbyte+=m;  
        allbyte-=m;  
       }  
          
       FileStream  fstr  =  new  FileStream(Path,FileMode.OpenOrCreate,FileAccess.Write);  
       fstr.Write(mbyte,0,startbyte);  //写文件 
       stream.Close();  
       fstr.Close(); 
      } 
  • 相关阅读:
    HDU 5059 Help him
    HDU 5058 So easy
    HDU 5056 Boring count
    HDU 5055 Bob and math problem
    HDU 5054 Alice and Bob
    HDU 5019 Revenge of GCD
    HDU 5018 Revenge of Fibonacci
    HDU 1556 Color the ball
    CodeForces 702D Road to Post Office
    CodeForces 702C Cellular Network
  • 原文地址:https://www.cnblogs.com/yanpo/p/2260274.html
Copyright © 2011-2022 走看看