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

    增加命名空间:
    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();
      }
    测试通过
  • 相关阅读:
    sql对日期操作
    computeroperationcommand
    Convert函数对日期的应用
    编写快速、高效的JavaScript代码
    vim常用操作技巧与配置
    PureFTPd安装配置
    关于PHP代码加密和文本加密
    父页面调用iframe里的js函数相关例程,Iframe之间通讯研究
    常用JavaScript语法100讲
    计算机端口
  • 原文地址:https://www.cnblogs.com/zhuor/p/507573.html
Copyright © 2011-2022 走看看