代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
asp.net web实现ftp上传代码(解决大文件上传问题)
作者:
Dezai.CN
/ 发布于
2012/3/16
/
614
原来使用asp.net上传控件上传 那个虽然简单但是页面不是很友好 然后就用了uploadify上传控件 这个控件虽然界面友好 但是大文件还是不能上传 而且在不同的浏览器会出现session丢失问题 所以我到了个ftp上传的方法
<div>using System;</div> <div>using System.Configuration;</div> <div>using System.Data;</div> <div>using System.Linq;</div> <div>using System.Web;</div> <div>using System.Web.Security;</div> <div>using System.Web.UI;</div> <div>using System.Web.UI.HtmlControls;</div> <div>using System.Web.UI.WebControls;</div> <div>using System.Web.UI.WebControls.WebParts;</div> <div>using System.Xml.Linq;</div> <div>using System.IO;</div> <div>using System.Net;</div> <div>using System.Text;</div> <div></div> <div>public partial class _Default : System.Web.UI.Page</div> <div>{</div> <div> //以下字段配置在web.config</div> <div> private string ftpServerIP = "127.0.0.1";//服务器ip</div> <div> private string ftpUserID = "FTPTEST";//用户名FTPTEST</div> <div> private string ftpPassword = "ftptest";//密码</div> <div> protected void Page_Load(object sender, EventArgs e)</div> <div> {</div> <div></div> <div> if (MyFile.Value != "")</div> <div> {</div> <div> //string a = MyFile.;</div> <div> } <div> </div> <div> } <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div> //ftp的上传功能</div> <div> private void Upload(string filename)</div> <div> {</div> <div> FileInfo fileInf = new FileInfo(filename);</div> <div></div> <div> string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;</div> <div> FtpWebRequest reqFTP;</div> <div></div> <div> // 根据uri创建FtpWebRequest对象</div> <div> reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));</div> <div></div> <div> // ftp用户名和密码</div> <div> reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);</div> <div></div> <div> // 默认为true,连接不会被关闭</div> <div>// 在一个命令之后被执行</div> <div> reqFTP.KeepAlive = false;</div> <div></div> <div> // 指定执行什么命令</div> <div> reqFTP.Method = WebRequestMethods.Ftp.UploadFile;</div> <div></div> <div> // 指定数据传输类型</div> <div> reqFTP.UseBinary = true;</div> <div></div> <div> // 上传文件时通知服务器文件的大小</div> <div> reqFTP.ContentLength = fileInf.Length;</div> <div></div> <div> // 缓冲大小设置为2kb</div> <div> int buffLength = 2048;</div> <div></div> <div> byte[] buff = new byte[buffLength];</div> <div> int contentLen;</div> <div></div> <div> // 打开一个文件流 (System.IO.FileStream) 去读上传的文件</div> <div> FileStream fs = fileInf.OpenRead();</div> <div> try</div> <div> {</div> <div> // 把上传的文件写入流</div> <div> Stream strm = reqFTP.GetRequestStream();</div> <div></div> <div> // 每次读文件流的2kb</div> <div> contentLen = fs.Read(buff, 0, buffLength);</div> <div></div> <div> // 流内容没有结束</div> <div> while (contentLen != 0)</div> <div> {</div> <div> // 把内容从file stream 写入 upload stream</div> <div> strm.Write(buff, 0, contentLen);</div> <div></div> <div> contentLen = fs.Read(buff, 0, buffLength);</div> <div> } <div></div> <div> // 关闭两个流</div> <div> strm.Close();</div> <div> fs.Close();</div> <div> this.Page.RegisterStartupScript("", "<script>alert('成功')</script>");</div> <div> } <div> catch (Exception ex)</div> <div> {</div> <div> // MessageBox.Show(ex.Message, "Upload Error");</div> <div> Response.Write("Upload Error:" + ex.Message);</div> <div> } <div> } <div></div> <div></div> <div> //从ftp服务器上下载文件的功能</div> <div> private void Download(string filePath, string fileName)</div> <div> {</div> <div> FtpWebRequest reqFTP;</div> <div></div> <div> try</div> <div> {</div> <div> FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);</div> <div></div> <div> reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));</div> <div></div> <div> reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;</div> <div></div> <div> reqFTP.UseBinary = true;</div> <div></div> <div> reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);</div> <div></div> <div> FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();</div> <div></div> <div> Stream ftpStream = response.GetResponseStream();</div> <div></div> <div> long cl = response.ContentLength;</div> <div></div> <div> int bufferSize = 2048;</div> <div></div> <div> int readCount;</div> <div></div> <div> byte[] buffer = new byte[bufferSize];</div> <div></div> <div> readCount = ftpStream.Read(buffer, 0, bufferSize);</div> <div></div> <div> while (readCount > 0)</div> <div> {</div> <div> outputStream.Write(buffer, 0, readCount);</div> <div></div> <div> readCount = ftpStream.Read(buffer, 0, bufferSize);</div> <div> } <div></div> <div> ftpStream.Close();</div> <div></div> <div> outputStream.Close();</div> <div></div> <div> response.Close();</div> <div> } <div> catch (Exception ex)</div> <div> {</div> <div> Response.Write("Download Error:" + ex.Message);</div> <div> } <div> } <div></div> <div> //从ftp服务器上获得文件列表</div> <div> public string[] GetFileList()</div> <div> {</div> <div> string[] downloadFiles;</div> <div> StringBuilder result = new StringBuilder();</div> <div> FtpWebRequest reqFTP;</div> <div> // HttpWebRequest reqFTP;</div> <div> try</div> <div> {</div> <div> reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));</div> <div> reqFTP.UseBinary = true;</div> <div> reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);</div> <div> reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;</div> <div> WebResponse response = reqFTP.GetResponse();</div> <div> StreamReader reader = new StreamReader(response.GetResponseStream());</div> <div> string line = reader.ReadLine();</div> <div> while (line != null)</div> <div> {</div> <div> result.Append(line);</div> <div> result.Append("\n");</div> <div> line = reader.ReadLine();</div> <div> } <div> // to remove the trailing '\n' </div> <div> result.Remove(result.ToString().LastIndexOf('\n'), 1);</div> <div> reader.Close();</div> <div> response.Close();</div> <div> return result.ToString().Split('\n');</div> <div> } <div> catch (Exception ex)</div> <div> {</div> <div> downloadFiles = null;</div> <div> return downloadFiles;</div> <div> } <div> } <div></div> <div> protected void Button1_Click(object sender, EventArgs e)</div> <div> {</div> <div> Upload("F:\\美国队长DVD中字.rmvb");</div> <div> } <div> protected void Button2_Click(object sender, EventArgs e)</div> <div> {</div> <div></div> <div> } <div>}
试试其它关键字
ftp上传
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3