zoukankan      html  css  js  c++  java
  • asp.net实现ftp上传代码(解决大文件上传问题)

        原来使用asp.net上传控件上传 那个虽然简单但是页面不是很友好 然后就用了uploadify上传控件  这个控件虽然界面友好 但是大文件还是不能上传 而且在不同的浏览器会出现session丢失问题 所以我到了个ftp上传的方法

        以下是具体代码

     
      1 using System;
    2 using System.Configuration;
    3 using System.Data;
    4 using System.Linq;
    5 using System.Web;
    6 using System.Web.Security;
    7 using System.Web.UI;
    8 using System.Web.UI.HtmlControls;
    9 using System.Web.UI.WebControls;
    10 using System.Web.UI.WebControls.WebParts;
    11 using System.Xml.Linq;
    12 using System.IO;
    13 using System.Net;
    14 using System.Text;
    15
    16 public partial class _Default : System.Web.UI.Page
    17 {
    18 //以下字段配置在web.config
    19 private string ftpServerIP = "127.0.0.1";//服务器ip
    20 private string ftpUserID = "FTPTEST";//用户名FTPTEST
    21 private string ftpPassword = "ftptest";//密码
    22 protected void Page_Load(object sender, EventArgs e)
    23 {
    24
    25 if (MyFile.Value != "")
    26 {
    27 //string a = MyFile.;
    28 }
    29
    30 }
    31
    32
    33
    34
    35
    36
    37
    38
    39 //ftp的上传功能
    40 private void Upload(string filename)
    41 {
    42 FileInfo fileInf = new FileInfo(filename);
    43
    44 string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
    45 FtpWebRequest reqFTP;
    46
    47 // 根据uri创建FtpWebRequest对象
    48 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
    49
    50 // ftp用户名和密码
    51 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
    52
    53 // 默认为true,连接不会被关闭
    54 // 在一个命令之后被执行
    55 reqFTP.KeepAlive = false;
    56
    57 // 指定执行什么命令
    58 reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
    59
    60 // 指定数据传输类型
    61 reqFTP.UseBinary = true;
    62
    63 // 上传文件时通知服务器文件的大小
    64 reqFTP.ContentLength = fileInf.Length;
    65
    66 // 缓冲大小设置为2kb
    67 int buffLength = 2048;
    68
    69 byte[] buff = new byte[buffLength];
    70 int contentLen;
    71
    72 // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
    73 FileStream fs = fileInf.OpenRead();
    74 try
    75 {
    76 // 把上传的文件写入流
    77 Stream strm = reqFTP.GetRequestStream();
    78
    79 // 每次读文件流的2kb
    80 contentLen = fs.Read(buff, 0, buffLength);
    81
    82 // 流内容没有结束
    83 while (contentLen != 0)
    84 {
    85 // 把内容从file stream 写入 upload stream
    86 strm.Write(buff, 0, contentLen);
    87
    88 contentLen = fs.Read(buff, 0, buffLength);
    89 }
    90
    91 // 关闭两个流
    92 strm.Close();
    93 fs.Close();
    94 this.Page.RegisterStartupScript("", "<script>alert('成功')</script>");
    95 }
    96 catch (Exception ex)
    97 {
    98 // MessageBox.Show(ex.Message, "Upload Error");
    99 Response.Write("Upload Error:" + ex.Message);
    100 }
    101 }
    102
    103
    104 //从ftp服务器上下载文件的功能
    105 private void Download(string filePath, string fileName)
    106 {
    107 FtpWebRequest reqFTP;
    108
    109 try
    110 {
    111 FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
    112
    113 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
    114
    115 reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
    116
    117 reqFTP.UseBinary = true;
    118
    119 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
    120
    121 FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
    122
    123 Stream ftpStream = response.GetResponseStream();
    124
    125 long cl = response.ContentLength;
    126
    127 int bufferSize = 2048;
    128
    129 int readCount;
    130
    131 byte[] buffer = new byte[bufferSize];
    132
    133 readCount = ftpStream.Read(buffer, 0, bufferSize);
    134
    135 while (readCount > 0)
    136 {
    137 outputStream.Write(buffer, 0, readCount);
    138
    139 readCount = ftpStream.Read(buffer, 0, bufferSize);
    140 }
    141
    142 ftpStream.Close();
    143
    144 outputStream.Close();
    145
    146 response.Close();
    147 }
    148 catch (Exception ex)
    149 {
    150 Response.Write("Download Error:" + ex.Message);
    151 }
    152 }
    153
    154 //从ftp服务器上获得文件列表
    155 public string[] GetFileList()
    156 {
    157 string[] downloadFiles;
    158 StringBuilder result = new StringBuilder();
    159 FtpWebRequest reqFTP;
    160 // HttpWebRequest reqFTP;
    161 try
    162 {
    163 reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
    164 reqFTP.UseBinary = true;
    165 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
    166 reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
    167 WebResponse response = reqFTP.GetResponse();
    168 StreamReader reader = new StreamReader(response.GetResponseStream());
    169 string line = reader.ReadLine();
    170 while (line != null)
    171 {
    172 result.Append(line);
    173 result.Append("\n");
    174 line = reader.ReadLine();
    175 }
    176 // to remove the trailing '\n'
    177 result.Remove(result.ToString().LastIndexOf('\n'), 1);
    178 reader.Close();
    179 response.Close();
    180 return result.ToString().Split('\n');
    181 }
    182 catch (Exception ex)
    183 {
    184 downloadFiles = null;
    185 return downloadFiles;
    186 }
    187 }
    188
    189 protected void Button1_Click(object sender, EventArgs e)
    190 {
    191 Upload("F:\\美国队长DVD中字.rmvb");
    192 }
    193 protected void Button2_Click(object sender, EventArgs e)
    194 {
    195
    196 }
    197 }
  • 相关阅读:
    机器学习系列(4) 线性回归
    MYSQL系列(5) 电商常用指标查询
    MYSQL系列(4) 关于时间函数的一些写法
    快速排序
    选择排序
    希尔排序
    直接插入排序
    冒泡排序
    Java修饰符大汇总
    字符串实现大数的相加
  • 原文地址:https://www.cnblogs.com/LYunF/p/2399927.html
Copyright © 2011-2022 走看看