asp.net web大文件传输 4G以上(调用CMD FTP)
2013-07-30 08:08
369 查看
using System.IO;
using System.Diagnostics;
public bool UploadFileToFTPByCmd(string LocalPath)
{
string _strFtpIP = "172.16.2.15";
string _strFtpPort = "21";
string _strFtpUserName = "test";
string _strFtpPassword = "test";
string _strFtpFoloderPath = @"D:\FTP";
if (!File.Exists(LocalPath))
{
this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('文件未生成成功,请检查!');</script>");
return false;
}
else
{
FileStream fs = null;
string _strScriptFolderPath = Path.GetDirectoryName(LocalPath) + "";
if (!Directory.Exists(_strScriptFolderPath))
{
Directory.CreateDirectory(_strScriptFolderPath);
}
string _strScriptFilePath = _strScriptFolderPath + @"\FtpScript" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt";
if (File.Exists(_strScriptFilePath))
{
fs = new FileStream(_strScriptFilePath, FileMode.Append);
}
else
{
fs = new FileStream(_strScriptFilePath, FileMode.Create);
}
string _strOpenFtpUrl = "open " + _strFtpIP + " " + _strFtpPort;
string _strCdFtpFolderPath = "cd " + _strFtpFoloderPath;
string _strPutFile = "put " + LocalPath;
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
sw.WriteLine("ftp");
sw.WriteLine(_strOpenFtpUrl);
sw.WriteLine(_strFtpUserName);
sw.WriteLine(_strFtpPassword);
sw.WriteLine(_strCdFtpFolderPath);
sw.WriteLine(_strPutFile);
sw.WriteLine("quit");
sw.WriteLine("exit");
sw.Close();
string _strCommand = @"FTP -v -i -s:" + _strScriptFilePath;
string _strReturnValue = RunCmd(_strCommand);
if (_strReturnValue.ToUpper().IndexOf("FILE RECEIVED OK") < 0)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('Ftp传输失败!');</script>");
return false;
}
}
return true;
}
public string RunCmd(string command)
{
Process p = null;
try
{
//实例一个Process类,启动一个独立进程
p = new Process();
//Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法,下面我们用到了他的几个属性:
p.StartInfo.FileName = "cmd.exe"; //设定程序名
p.StartInfo.Arguments = "/c " + command; //设定程式执行参数
p.StartInfo.UseShellExecute = false; //关闭Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向标准输入
p.StartInfo.RedirectStandardOutput = true; //重定向标准输出
p.StartInfo.RedirectStandardError = true; //重定向错误输出
p.StartInfo.CreateNoWindow = true; //设置不显示窗口
p.Start(); //启动
p.StandardInput.WriteLine("exit"); //不过要记得加上Exit要不然下一行程式执行的时候会当机
return p.StandardOutput.ReadToEnd(); //从输出流取得命令执行结果
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (p != null)
p.Close();
}
}
//调用
protected void btnCMDFTP_Click(object sender, EventArgs e)
{
if (txtFileUpload.HasFile)
{
if (UploadFileToFTPByCmd(txtFileUpload.PostedFile.FileName))
{
this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('上传成功!');</script>");
}
}
}
优点:基本可以实现,速度不错,无需要安装第三方软件
缺点:无法隐藏FTP账号,无法重命名文件
using System.Diagnostics;
public bool UploadFileToFTPByCmd(string LocalPath)
{
string _strFtpIP = "172.16.2.15";
string _strFtpPort = "21";
string _strFtpUserName = "test";
string _strFtpPassword = "test";
string _strFtpFoloderPath = @"D:\FTP";
if (!File.Exists(LocalPath))
{
this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('文件未生成成功,请检查!');</script>");
return false;
}
else
{
FileStream fs = null;
string _strScriptFolderPath = Path.GetDirectoryName(LocalPath) + "";
if (!Directory.Exists(_strScriptFolderPath))
{
Directory.CreateDirectory(_strScriptFolderPath);
}
string _strScriptFilePath = _strScriptFolderPath + @"\FtpScript" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".txt";
if (File.Exists(_strScriptFilePath))
{
fs = new FileStream(_strScriptFilePath, FileMode.Append);
}
else
{
fs = new FileStream(_strScriptFilePath, FileMode.Create);
}
string _strOpenFtpUrl = "open " + _strFtpIP + " " + _strFtpPort;
string _strCdFtpFolderPath = "cd " + _strFtpFoloderPath;
string _strPutFile = "put " + LocalPath;
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312"));
sw.WriteLine("ftp");
sw.WriteLine(_strOpenFtpUrl);
sw.WriteLine(_strFtpUserName);
sw.WriteLine(_strFtpPassword);
sw.WriteLine(_strCdFtpFolderPath);
sw.WriteLine(_strPutFile);
sw.WriteLine("quit");
sw.WriteLine("exit");
sw.Close();
string _strCommand = @"FTP -v -i -s:" + _strScriptFilePath;
string _strReturnValue = RunCmd(_strCommand);
if (_strReturnValue.ToUpper().IndexOf("FILE RECEIVED OK") < 0)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('Ftp传输失败!');</script>");
return false;
}
}
return true;
}
public string RunCmd(string command)
{
Process p = null;
try
{
//实例一个Process类,启动一个独立进程
p = new Process();
//Process类有一个StartInfo属性,这个是ProcessStartInfo类,包括了一些属性和方法,下面我们用到了他的几个属性:
p.StartInfo.FileName = "cmd.exe"; //设定程序名
p.StartInfo.Arguments = "/c " + command; //设定程式执行参数
p.StartInfo.UseShellExecute = false; //关闭Shell的使用
p.StartInfo.RedirectStandardInput = true; //重定向标准输入
p.StartInfo.RedirectStandardOutput = true; //重定向标准输出
p.StartInfo.RedirectStandardError = true; //重定向错误输出
p.StartInfo.CreateNoWindow = true; //设置不显示窗口
p.Start(); //启动
p.StandardInput.WriteLine("exit"); //不过要记得加上Exit要不然下一行程式执行的时候会当机
return p.StandardOutput.ReadToEnd(); //从输出流取得命令执行结果
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (p != null)
p.Close();
}
}
//调用
protected void btnCMDFTP_Click(object sender, EventArgs e)
{
if (txtFileUpload.HasFile)
{
if (UploadFileToFTPByCmd(txtFileUpload.PostedFile.FileName))
{
this.ClientScript.RegisterStartupScript(this.GetType(), "alert", "<script>alert('上传成功!');</script>");
}
}
}
优点:基本可以实现,速度不错,无需要安装第三方软件
缺点:无法隐藏FTP账号,无法重命名文件
相关文章推荐
- Asp.net C#下实现运行cmd调用命令的代码实例
- ASP.NET母版页中调用内容页的方法和web用户控件调用.aspx页面里的方法
- Jquery 调用asp.net ajax (web service/static page method)的示例(二)---复杂参数
- 在asp.net 2.0中的web.config文件中调用外部文件
- 巧用ASP.NET预编译Web应用程序规避调用延迟的方法
- jQuery AJax调用asp.net webservers的实现代码
- ASP.NET WebService+Ajax调用
- 使用asp.net FtpWebRequest 实现FTP常用功能
- ASP.Net:Javascript 通过PageMethods 调用后端WebMethod方法 + 多线程数据处理 示例
- JQuery直接调用asp.net后台WebMethod方法
- Asp.Net中Web.Config中的数据库连接字符串的调用方法(初学者实例)
- asp.net 2.0中不同web控件之间的相互调用
- ASP.NET中使用HttpWebRequest调用WCF
- 在asp.net(c#)下实现调用cmd
- asp.net 2.0中不同web控件之间的相互调用
- 如何使用来自 ASP.NET 的客户端证书调用 Web 服务
- 用 ASP.NET AJAX 调用Web 服务
- JQuery直接调用asp.net后台WebMethod方法
- ASP.NET Web服务调用发生错误,错误代码404
- asp.net调用远程Web_Service(服务引用方式)