C# FTP上传文件 绝对能用

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace ftp
{
    public partial class Form1 : Form
    {
        string ftpURI;
        string ftpUserID;
        string ftpPassword;
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fg = new OpenFileDialog();
            fg.RestoreDirectory = true;
            if (fg.ShowDialog() == DialogResult.OK)
            {
                
                if (Upload(fg.FileName))
                    MessageBox.Show("上传成功");
                else
                    MessageBox.Show("上传失败");
            }
        }
        long t = ConvertDateTimeToInt(DateTime.Now);
        public bool Upload(string filename)
        {
            FileInfo fileInf = new FileInfo(filename);
          
            string uri = ftpURI +t+ fileInf.Name.Substring(fileInf.Name.LastIndexOf("."));
            MessageBox.Show(uri);
            FtpWebRequest reqFTP;


            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            reqFTP.KeepAlive = false;
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
            reqFTP.UseBinary = true;
            reqFTP.ContentLength = fileInf.Length;
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
            FileStream fs = fileInf.OpenRead();
            try
            {
                Stream strm = reqFTP.GetRequestStream();
                contentLen = fs.Read(buff, 0, buffLength);
                while (contentLen != 0)
                {
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);
                }
                strm.Close();
                fs.Close();
                fs.Dispose();
                return true;
            }
            catch (Exception ex)
            {
                fs.Close();
                fs.Dispose();
                return false;
             
            }
        }
        public static long ConvertDateTimeToInt(System.DateTime time)
        {
            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1, 0, 0, 0, 0));
            long t = (time.Ticks - startTime.Ticks) / 10000;   //除10000调整为13位      
            return t;
        }


        private void Form1_Load(object sender, EventArgs e)
        {
           // 上传到ftp的位置
            ftpURI = @"";
            //用户名
            ftpUserID = "";
            //密码
            ftpPassword = "";
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值