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();
      }
    测试通过
  • 相关阅读:
    修改MySQL表中自增编号
    springboot整合mybatis统一配置bean的别名
    kotlin来了!!
    微信小程序获取登录手机号
    maven--package
    修改oracle数据库时间
    oracle启动停止命令
    安装 MySQL 之后初始密码在哪里??
    EntityFramework~~~三种模式
    webqq协议分析之~~~~验证是否需要验证码
  • 原文地址:https://www.cnblogs.com/zhuor/p/507573.html
Copyright © 2011-2022 走看看