using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    //以下字段配置在web.config
    private string ftpServerIP = "127.0.0.1";//服务器ip
    private string ftpUserID = "FTPTEST";//用户名FTPTEST
    private string ftpPassword = "ftptest";//密码
    protected void Page_Load(object sender, EventArgs e)
    {

if (MyFile.Value != "")
        {
            //string a = MyFile.;
        }
       
    }

//ftp的上传功能
    private void Upload(string filename)
    {
        FileInfo fileInf = new FileInfo(filename);

string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
        FtpWebRequest reqFTP;

// 根据uri创建FtpWebRequest对象
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));

// ftp用户名和密码
        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

// 默认为true,连接不会被关闭
// 在一个命令之后被执行
        reqFTP.KeepAlive = false;

// 指定执行什么命令
        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

// 指定数据传输类型
        reqFTP.UseBinary = true;

// 上传文件时通知服务器文件的大小
        reqFTP.ContentLength = fileInf.Length;

// 缓冲大小设置为2kb
        int buffLength = 2048;

byte[] buff = new byte[buffLength];
        int contentLen;

// 打开一个文件流 (System.IO.FileStream) 去读上传的文件
        FileStream fs = fileInf.OpenRead();
        try
        {
            // 把上传的文件写入流
            Stream strm = reqFTP.GetRequestStream();

// 每次读文件流的2kb
            contentLen = fs.Read(buff, 0, buffLength);

// 流内容没有结束
            while (contentLen != 0)
            {
                // 把内容从file stream 写入 upload stream
                strm.Write(buff, 0, contentLen);

contentLen = fs.Read(buff, 0, buffLength);
            }

// 关闭两个流
            strm.Close();
            fs.Close();
            this.Page.RegisterStartupScript("", "<script>alert('成功')</script>");
        }
        catch (Exception ex)
        {
            // MessageBox.Show(ex.Message, "Upload Error");
            Response.Write("Upload Error:" + ex.Message);
        }
    }

//从ftp服务器上下载文件的功能
    private void Download(string filePath, string fileName)
    {
        FtpWebRequest reqFTP;

try
        {
            FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);

reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));

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 = 2048;

int readCount;

byte[] buffer = new byte[bufferSize];

readCount = ftpStream.Read(buffer, 0, bufferSize);

while (readCount > 0)
            {
                outputStream.Write(buffer, 0, readCount);

readCount = ftpStream.Read(buffer, 0, bufferSize);
            }

ftpStream.Close();

outputStream.Close();

response.Close();
        }
        catch (Exception ex)
        {
            Response.Write("Download Error:" + ex.Message);
        }
    }

//从ftp服务器上获得文件列表
    public string[] GetFileList()
    {
        string[] downloadFiles;
        StringBuilder result = new StringBuilder();
        FtpWebRequest reqFTP;
        // HttpWebRequest reqFTP;
        try
        {
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
            WebResponse response = reqFTP.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string line = reader.ReadLine();
            while (line != null)
            {
                result.Append(line);
                result.Append("\n");
                line = reader.ReadLine();
            }
            // to remove the trailing '\n'       
            result.Remove(result.ToString().LastIndexOf('\n'), 1);
            reader.Close();
            response.Close();
            return result.ToString().Split('\n');
        }
        catch (Exception ex)
        {
            downloadFiles = null;
            return downloadFiles;
        }
    }

protected void Button1_Click(object sender, EventArgs e)
    {
        Upload("F:\\美国队长DVD中字.rmvb");
    }
    protected void Button2_Click(object sender, EventArgs e)
    {

}
}

20160113006 asp.net实现ftp上传代码(解决大文件上传问题)的更多相关文章

  1. Asp.Net Core 轻松学-一行代码搞定文件上传 JSONHelper

    Asp.Net Core 轻松学-一行代码搞定文件上传   前言     在 Web 应用程序开发过程中,总是无法避免涉及到文件上传,这次我们来聊一聊怎么去实现一个简单方便可复用文件上传功能:通过创建 ...

  2. Asp.Net Core 轻松学-一行代码搞定文件上传

    前言     在 Web 应用程序开发过程中,总是无法避免涉及到文件上传,这次我们来聊一聊怎么去实现一个简单方便可复用文件上传功能:通过创建自定义绑定模型来实现文件上传. 1. 实现自定义绑定模型 1 ...

  3. windows下IIS+PHP解决大文件上传500错问题

    linux下改到iis+php后,上传大于2M就出500错,改了php.ini中的upload_max_filesize也不行,最后解决如下: 第一步:修改php.ini 上传大小限制 (以上传500 ...

  4. java大文件上传解决方案

    最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...

  5. Web大文件上传断点续传解决方案

    最近遇见一个需要上传百兆大文件的需求,调研了七牛和腾讯云的切片分段上传功能,因此在此整理前端大文件上传相关功能的实现. 在某些业务中,大文件上传是一个比较重要的交互场景,如上传入库比较大的Excel表 ...

  6. 绝对好用Flash多文件大文件上传控件

    本实例采用的是Uploadify上传插件,.NET程序,源程序是从网上找的,但是有Bug,已经修改好,并标有部分注释.绝对好用,支持单文件.多文件上传,支持大文件上传,已经过多方面测试,保证好用. 以 ...

  7. Asp.net mvc 大文件上传 断点续传

    Asp.net mvc 大文件上传 断点续传 进度条   概述 项目中需要一个上传200M-500M的文件大小的功能,需要断点续传.上传性能稳定.突破asp.net上传限制.一开始看到51CTO上的这 ...

  8. Asp.net mvc 大文件上传 断点续传 进度条

    概述 项目中需要一个上传200M-500M的文件大小的功能,需要断点续传.上传性能稳定.突破asp.net上传限制.一开始看到51CTO上的这篇文章,此方法确实很不错,能够稳定的上传大文件,http: ...

  9. ASP.NET 大文件上传的简单处理

    在 ASP.NET 开发的过程中,文件上传往往使用自带的 FileUpload 控件,可是用过的人都知道,这个控件的局限性十分大,最大的问题就在于上传大文件时让开发者尤为的头疼,而且,上传时无法方便的 ...

随机推荐

  1. tomcat deploy部署项目三种方法

    1.将应用文件夹或war文件直接copy到tomcat的webapps目录下,这样tomcat启动的时候会将webapps目录下的文件夹或war文件的内容当成应用部署.这种方式最简单且无须书写任何配置 ...

  2. UltraEdit常用配置&搭建Java/C开发环境

    一:个人使用UE期间总结了以下经常使用的配置 1.手动配置语法高亮 [高级]->[配置]->[编辑器显示]->[语法高亮]->[词语列表的完整路径]->[浏览]找到安装目 ...

  3. M面经Prepare: Delete Words Starting With One Character

    给定一个char array, 这个array是一个句子,然后给定一个字母,把这个array里面带有这个字母开头的单次删掉,操作是要求in place. 检测   array[i]==' ' & ...

  4. C++builder 图像字符流的存储和加载

    __fastcall TForm6::TForm6(TComponent* Owner) : TForm(Owner) { #if 1 //for debug AllocConsole(); Atta ...

  5. bat file handling, main: echo type *.txt >> />

    echo.@.call.pause.rem(小技巧:用::代替rem)是批处理文件最常用的几个命令 echo 表示显示此命令后的字符  echo off 表示在此语句后所有运行的命令都不显示命令行本身 ...

  6. [转]数据库高可用架构(MySQL、Oracle、MongoDB、Redis)

    一.MySQL   MySQL小型高可用架构 方案:MySQL双主.主从 + Keepalived主从自动切换 服务器资源:两台PC Server 优点:架构简单,节省资源 缺点:无法线性扩展,主从失 ...

  7. [转]SecureCRT使用配置详细图文教程

        Secure CRT是一款支持 SSH2.SSH1.Telnet.Telnet/SSH.Relogin.Serial.TAPI.RAW 等协议的终端仿真程序,最吸引我的是,SecureCRT ...

  8. HDU 4899 Hero meet devil(状压DP)(2014 Multi-University Training Contest 4)

    Problem Description There is an old country and the king fell in love with a devil. The devil always ...

  9. bzoj4216 Pig

    水题,题目难点大概就是空间限制上了,开longlong会爆,可以开个int数组求前缀和,然后一旦绝对值超过20亿,则将其取模,并记录下当前位置,这样询问时就可以二分这些超过的位置,将其乘以20亿后加上 ...

  10. 生成apache证书(https应用)

    # cd /usr/local/apache2/conf# tar zxvf ssl.ca-0.1.tar.gz# cd ssl.ca-0.1生成根证书:# ./new-root-ca.sh      ...