zoukankan      html  css  js  c++  java
  • asp.net web大文件传输 4G以上(调用CMD FTP)

    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账号,无法重命名文件

  • 相关阅读:
    《编写高质量代码Web前端开发修炼之道》(读书笔记) windrainpy
    jQuery琐碎笔记 windrainpy
    【记录】事件冒泡和事件捕获 windrainpy
    JS比较容易迷惑的几个地方 windrainpy
    【转】成为顶级程序员的唯一途径! windrainpy
    jquery键盘事件 windrainpy
    【转】IE绝对定位元素神秘消失或被遮挡的解决 windrainpy
    简单算法
    HTTP的状态
    css的兼容问题
  • 原文地址:https://www.cnblogs.com/zhangxiaozhong/p/3224276.html
Copyright © 2011-2022 走看看