您的位置:首页 > 编程语言 > ASP

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账号,无法重命名文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: