zoukankan      html  css  js  c++  java
  • FTP服务器上的文件下载及下载文件为什么会包含当前页面的HTML代码

      public bool Download(string fileName)
        {
    
            string serverDir = "";//ftp服务器上的文件夹
            string ftpServerIP = "";//服务器ip
            string ftpUserID = "";//用户名
            string ftpPassword = "";//密码
           
            string uri = string.Format("ftp://{0}/{1}/{2}", ftpServerIP, serverDir, fileName);
            FtpWebRequest reqFTP;
            try
            {
             
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                 int bufferSize = 102400;
                int readCount = 0;
                byte[] buffer = new byte[bufferSize];
                Response.ContentType = "application/octet-stream";
                //通知浏览器下载文件而不是打开    
                Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
               while ((readCount = ftpStream.Read(buffer, 0, bufferSize)) > 0)
                {
                    Response.OutputStream.Write(buffer, 0, readCount);
                   //Response.Flush();
                     
                }
              
                 Response.End();
                 ftpStream.Close();
                 response.Close();
                 return true;
            }
            catch (Exception ex)
            {
             return false;
          
            }

    PS:

    通常页面的点击事件进入后台程序通常会进行页面刷新,但是当我点击下载文件的时候为什么不刷新当前页面?
    asp.net 中aspx页面实际是一个类,该类中是服务器通过HTTP中的Response响应向客户端浏览器write()HTML拼接的代码数据,通过Response.ContentType = "application/html"; 来指定浏览器发送的数据,浏览器就会把他翻译成需要显示的页面。Response.ContentType="application/数据类型"是用来指定发送的数据类型,当发送字节流数据时,响应类型指定为Response.ContentType = "application/octet-stream"; 这样的话就会客户端浏览器就会知道服务器的响应信息是字节流文件就不会重新加载HTML就不会产生刷新,浏览器显示的页面就是刚进入下载界面时候服务器Response.write()的HTML代码,如果不加Response.End();的话,服务器就会继续使用response.write()输出该页面的HTML代码并加入Response.OutputStream.Write()中,这样打开下载后的文件就会发现内部多了当前页面的HTML代码,可以理解为aspx页面类是服务器响应Response.write()输出的一部分.  

    个人分析,如有不对请指正。

  • 相关阅读:
    Apache、IIS、Nginx等绝大多数web服务器,都不允许静态文件响应POST请求
    转载:Struts2+Jquery实现ajax并返回json类型数据
    div 添加滚动条
    jsp页面 如何通过el表达式获取request属性值
    【转】通过Hibernate将数据 存入oracle数据库例子
    jsp 中 有没有类似java if else语句
    IDEA使用(一)
    Git进阶(二)
    JS语法记录
    Debian之MySQL
  • 原文地址:https://www.cnblogs.com/eason-chan/p/3668623.html
Copyright © 2011-2022 走看看