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. 单位px 转换成 rem

    <script type="text/javascript"> var oHtml = document.documentElement; getSize(); win ...

  2. zend optimizer在wamp的基础上安装

    在用wampserver集成开发环境下,有时会碰到一些开源程序需要zend optimizer的支持,下面我用的wamp的版本是2.0,optimizer的版本是ZendOptimizer-3.3.3 ...

  3. ofbiz进击 。 ofbiz 退货流程(包含获取可退货项流程分析 以及 取消退货项的过程分析)

    根据订单获取可退货项流程分析 退货的时候,调用 services_return.xml 中的获取可进行退货的退货项  getReturnableItems  ,该服务调用了Java类 org.ofbi ...

  4. JSon_零基础_004_将Set集合对象转换为JSon格式的对象字符串,返回给界面

    将Set集合对象转换为JSon格式的对象字符串,返回给界面 需要导入的jar包: 编写:servlet: package com.west.webcourse.servlet; import java ...

  5. git批量删除分支

    要删除本地,首先要考虑以下三点 列出所有本地分支 搜索目标分支如:所有含有'dev'的分支 将搜索出的结果传给删除函数 所以我们可以得到: git br |grep 'dev' |xargs git ...

  6. spark小技巧-mapPartitions

    与map方法类似,map是对rdd中的每一个元素进行操作,而mapPartitions(foreachPartition)则是对rdd中的每个分区的迭代器进行操作.如果在map过程中需要频繁创建额外的 ...

  7. paper 55:图像分割代码汇总

    matlab 图像分割算法源码 1.图像反转 MATLAB程序实现如下:I=imread('xian.bmp');J=double(I);J=-J+(256-1); %图像反转线性变换H=uint8( ...

  8. 前端构建工具gulp入门

    本文假设你之前没有用过任何任务脚本(task runner)和命令行工具,一步步教你上手Gulp.不要怕,它其实很简单,我会分为五步向你介绍gulp并帮助你完成一些惊人的事情.那就直接开始吧. 第一步 ...

  9. js 获取select 中option 的个数

    //document.writeln(document.getElementById("sel").options.length); //document.writeln(docu ...

  10. windows下快速启动 nginx 和 php-cgi 的两个批处理

    这是启动的批处理: set nginx=D:\nginx-1.9.5\ set php=D:\php\ start /MIN %nginx%nginx.exe start /MIN %php%php- ...