.net ftp上传文件方法
2016-01-15 13:49
671 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Text;
using System.Configuration;
namespace DlnOffice.general.netdisk
{
public partial class fileupload2 : System.Web.UI.Page
{
//这三个常量是从webconfig获取
private string ftpServerIP = ConfigurationManager.AppSettings["ftpServerIP"];
private string ftpUserID = ConfigurationManager.AppSettings["ftpUserID"];
private string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"];
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
up_FTP(FileUpload1);
//up_FTP(FileUpload2);
Response.Write("<script>alert('完成');window.location.href ='fileupload2.aspx'</script>");
//else
//{
// Response.Write("<script>alert('未选择文件');</script>");
// return;
//}
}
public void up_FTP(FileUpload fu)
{
string AppPath = (base.Request.QueryString["parent_folder"] != null) ? base.Request.QueryString["parent_folder"].ToString() : "/信息科/常用软件";
if (string.IsNullOrEmpty(AppPath))
{
base.Response.Redirect("index.aspx");
return;
}
//文件上传地址根目录,这里通过IIS架设本地主机为FTP服务器
//string FileSaveUri = @"ftp://127.0.0.1/";
string FileSaveUri = @"ftp://" + ftpServerIP + AppPath + "/";
//FTP用户名密码,就是本机的用户名密码
string ftpUser = ftpUserID;
string ftpPassWord = ftpPassword;
Stream requestStream = null;
Stream fileStream = null;
FtpWebResponse uploadResponse = null;//创建FtpWebResponse实例uploadResponse
//Btn_Upload.
if (fu.HasFile)
{
//获取文件长度
int FileLength = fu.PostedFile.ContentLength;
//限制上传文件最大不能超过1G
if (FileLength < 1024 * 1024 * 1024)
{
try
{
//格式化为URI
Uri uri = new Uri(FileSaveUri + Path.GetFileName(fu.PostedFile.FileName));
FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uri);//创建FtpWebRequest实例uploadRequest
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;//将FtpWebRequest属性设置为上传文件
uploadRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);//认证FTP用户名密码
requestStream = uploadRequest.GetRequestStream();//获得用于上传FTP的流
byte[] buffer = new byte[FileLength];
fileStream = fu.PostedFile.InputStream;//截取FileUpload获取的文件流,作为上传FTP的流
fileStream.Read(buffer, 0, FileLength);
requestStream.Write(buffer, 0, FileLength);//将buffer写入流
requestStream.Close();
uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();//返回FTP服务器响应,上传完成
//上传成功
}
catch (Exception ex)
{
//无法上传
Response.Write("<script>alert('无法上传');</script>");
return;
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
}//end if #FileLength#
else
{
//上传文件过大
Response.Write("<script>alert('上传文件过大,文件不得超过1G');</script>");
return;
}
}//end if #FileUpload.HasFile#
else
{
//未选择文件
return;
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Text;
using System.Configuration;
namespace DlnOffice.general.netdisk
{
public partial class fileupload2 : System.Web.UI.Page
{
//这三个常量是从webconfig获取
private string ftpServerIP = ConfigurationManager.AppSettings["ftpServerIP"];
private string ftpUserID = ConfigurationManager.AppSettings["ftpUserID"];
private string ftpPassword = ConfigurationManager.AppSettings["ftpPassword"];
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
up_FTP(FileUpload1);
//up_FTP(FileUpload2);
Response.Write("<script>alert('完成');window.location.href ='fileupload2.aspx'</script>");
//else
//{
// Response.Write("<script>alert('未选择文件');</script>");
// return;
//}
}
public void up_FTP(FileUpload fu)
{
string AppPath = (base.Request.QueryString["parent_folder"] != null) ? base.Request.QueryString["parent_folder"].ToString() : "/信息科/常用软件";
if (string.IsNullOrEmpty(AppPath))
{
base.Response.Redirect("index.aspx");
return;
}
//文件上传地址根目录,这里通过IIS架设本地主机为FTP服务器
//string FileSaveUri = @"ftp://127.0.0.1/";
string FileSaveUri = @"ftp://" + ftpServerIP + AppPath + "/";
//FTP用户名密码,就是本机的用户名密码
string ftpUser = ftpUserID;
string ftpPassWord = ftpPassword;
Stream requestStream = null;
Stream fileStream = null;
FtpWebResponse uploadResponse = null;//创建FtpWebResponse实例uploadResponse
//Btn_Upload.
if (fu.HasFile)
{
//获取文件长度
int FileLength = fu.PostedFile.ContentLength;
//限制上传文件最大不能超过1G
if (FileLength < 1024 * 1024 * 1024)
{
try
{
//格式化为URI
Uri uri = new Uri(FileSaveUri + Path.GetFileName(fu.PostedFile.FileName));
FtpWebRequest uploadRequest = (FtpWebRequest)WebRequest.Create(uri);//创建FtpWebRequest实例uploadRequest
uploadRequest.Method = WebRequestMethods.Ftp.UploadFile;//将FtpWebRequest属性设置为上传文件
uploadRequest.Credentials = new NetworkCredential(ftpUser, ftpPassWord);//认证FTP用户名密码
requestStream = uploadRequest.GetRequestStream();//获得用于上传FTP的流
byte[] buffer = new byte[FileLength];
fileStream = fu.PostedFile.InputStream;//截取FileUpload获取的文件流,作为上传FTP的流
fileStream.Read(buffer, 0, FileLength);
requestStream.Write(buffer, 0, FileLength);//将buffer写入流
requestStream.Close();
uploadResponse = (FtpWebResponse)uploadRequest.GetResponse();//返回FTP服务器响应,上传完成
//上传成功
}
catch (Exception ex)
{
//无法上传
Response.Write("<script>alert('无法上传');</script>");
return;
}
finally
{
if (uploadResponse != null)
uploadResponse.Close();
if (fileStream != null)
fileStream.Close();
if (requestStream != null)
requestStream.Close();
}
}//end if #FileLength#
else
{
//上传文件过大
Response.Write("<script>alert('上传文件过大,文件不得超过1G');</script>");
return;
}
}//end if #FileUpload.HasFile#
else
{
//未选择文件
return;
}
}
}
}
相关文章推荐
- Sublime text 2/3 [Decode error - output not utf-8] 完美解决方法
- ubuntu 14.04中打开vsftpd服务步骤
- PHP页面间参数传递的四种方法详解
- 使用虚拟主机配置phpmyadmin, wordpress, discuz
- Laravel Cache 使用
- thinkphp验证码
- php一点笔记
- 写一下ubuntu下的vsftpd吧
- 关掉rockmongo因PHP版本问题导致的警告显示
- PHP处理十六进制穿上来的图片(IOS、and端)
- php文字替换表情
- PHP将字符串写入txt文件
- GD 库的使用
- PHP的正则匹配小结
- Php 删除目录
- php storm漂亮主题设置
- 【Ajax】PHP中ajax的基本知识点
- phpStorm入门
- sublime text 3:创建可重复用的代码片段php文件头部注释信息快速生成
- PHPUnit从零开始(1):从它的安装配置说起