FTP文件上传

一、批量文件上传。

        //FtpURL:要上传到的FTP路径。

        Uri uriAddress = new Uri(FtpURL);

        //filename:上传前的文件名数组;filenames:上传后的文件名数组;UserName:FTP用户名; PassWord:FTP密码。

        FileCopys(filename, uriAddress, filenames, UserName, PassWord);

 

        #region 数据批量上传
        protected void FileCopys(string[] fileName, Uri uploadUrl, string[] fileNameS, string username, string password)
        {
            try
            {
                for (int i = 0; i < fileNameS.Length; i++)
                {
                    if (uploadUrl.Scheme == Uri.UriSchemeFtp)
                    {
                        FileInfo fileInf = new FileInfo(fileName[i]);
                        FtpWebRequest reqFTP;
                        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uploadUrl + fileNameS[i]));
                        reqFTP.Credentials = new NetworkCredential(username, password);
                        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                        reqFTP.KeepAlive = false;
                        reqFTP.UseBinary = false;
                        reqFTP.ContentLength = fileInf.Length;
                        int buffLength = 2048;
                        byte[] buff = new byte[buffLength];
                        int contentLen;
                        FileStream fs = fileInf.OpenRead();
                        Stream strm = reqFTP.GetRequestStream();
                        contentLen = fs.Read(buff, 0, buffLength);
                        int allbye = (int)fileInf.Length;
                        int startbye = 0;
                        while (contentLen != 0)
                        {
                            strm.Write(buff, 0, contentLen);
                            contentLen = fs.Read(buff, 0, buffLength);
                            startbye += contentLen;
                        }
                        strm.Close();
                        fs.Close();
                    }
                }
            }
            catch
            {
               
            }
        }
        #endregion

二、单个文件上传

        //FtpURL:要上传到的FTP路径。

        Uri uriAddress = new Uri(FtpURL);

        //filename:需要上传的文件名;filename:上传后的文件名;UserName:FTP用户名; PassWord:FTP密码。

        FileCopy(filename, uriAddress, filename, UserName, PassWord);

 

        protected void FileCopy(string fileName, Uri uploadUrl, string fileNameS, string username, string password)
        {
            //FileCopy(InitFile1, uriAddress, FileNames1, UserName, PassWord);
            //FileCopy(InitFile5, uriAddress, FileNames5, UserName, PassWord);
            //FileCopy(InitFile6, uriAddress, FileNames6, UserName, PassWord);
            //FileCopy(InitFile7, uriAddress, FileNames7, UserName, PassWord);
            //FileCopy(InitFile8, uriAddress, FileNames8, UserName, PassWord);
            //FileCopy(InitFile9, uriAddress, FileNames9, UserName, PassWord);
            //FileCopy(InitFile11, uriAddress, FileNames11, UserName, PassWord);
            try
            {
                if (uploadUrl.Scheme == Uri.UriSchemeFtp)
                {
                    FileInfo fileInf = new FileInfo(fileName);
                    FtpWebRequest reqFTP;
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uploadUrl + fileNameS));
                    reqFTP.Credentials = new NetworkCredential(username, password);
                    reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                    reqFTP.KeepAlive = false;
                    reqFTP.UseBinary = false;
                    reqFTP.ContentLength = fileInf.Length;
                    int buffLength = 2048;
                    byte[] buff = new byte[buffLength];
                    int contentLen;
                    FileStream fs = fileInf.OpenRead();
                    Stream strm = reqFTP.GetRequestStream();
                    contentLen = fs.Read(buff, 0, buffLength);
                    int allbye = (int)fileInf.Length;
                    int startbye = 0;
                    while (contentLen != 0)
                    {
                        strm.Write(buff, 0, contentLen);
                        contentLen = fs.Read(buff, 0, buffLength);
                        startbye += contentLen;
                    }
                    strm.Close();
                    fs.Close();
                }
            }
            catch
            {
                //MessageBox.Show("FTP站点用户名或密码错误,请与管理员联系!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            }
        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值