• 大小: 48KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-17
  • 语言: C#
  • 标签: FTP上传  

资源简介

基于C#的FTP上传Demo,基于FTP协议,无需服务端应用,只需服务端建立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 FTPUpload
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private string _fName = ““;
        private string _ftpfilepath = ““;

        private void button1_Click(object sender EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                _fName = openFileDialog1.FileName;
                _ftpfilepath = openFileDialog1.SafeFileName;
                lbl_text.Text = openFileDialog1.SafeFileName;
            }
        }

        private void button2_Click(object sender EventArgs e)
        {
            MessageBox.Show(Ftpfile(_ftpfilepath _fName));
        }

        /// 
        /// 上传
        /// 

        /// 文件名
        /// 文件全路径
        /// 
        public string Ftpfile(string ftpfilepath string inputfilepath)
        {
            try
            {
                string ftphost = “127.0.0.1:21“;//服务端IP

                string ftpfullpath = “ftp://“ + ftphost + “/“ + ftpfilepath;
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
                ftp.Credentials = new NetworkCredential(“登录名“ “登录密码“);

                ftp.KeepAlive = true;
                ftp.UseBinary = true;
                ftp.Method = WebRequestMethods.Ftp.UploadFile;
                FileStream fs = File.OpenRead(inputfilepath);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer 0 buffer.Length);
                fs.Close();
                Stream ftpstream = ftp.GetRequestStream();//上传
                ftpstream.Write(buffer 0 buffer.Length);
                ftpstream.Close();
                return “成功“;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }

        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

    ..A..H.     38400  2017-12-15 14:32  FTPUpload\.vs\FTPUpload\v14\.suo

     文件        189  2017-12-15 14:25  FTPUpload\FTPUpload\App.config

     文件       9728  2017-12-15 14:32  FTPUpload\FTPUpload\bin\Debug\FTPUpload.exe

     文件        189  2017-12-15 14:25  FTPUpload\FTPUpload\bin\Debug\FTPUpload.exe.config

     文件      19968  2017-12-15 14:32  FTPUpload\FTPUpload\bin\Debug\FTPUpload.pdb

     文件      22696  2017-12-15 14:30  FTPUpload\FTPUpload\bin\Debug\FTPUpload.vshost.exe

     文件        189  2017-12-15 14:25  FTPUpload\FTPUpload\bin\Debug\FTPUpload.vshost.exe.config

     文件        490  2017-03-19 05:00  FTPUpload\FTPUpload\bin\Debug\FTPUpload.vshost.exe.manifest

     文件       2344  2017-12-15 14:32  FTPUpload\FTPUpload\Form1.cs

     文件       3466  2017-12-15 14:29  FTPUpload\FTPUpload\Form1.Designer.cs

     文件       6020  2017-12-15 14:29  FTPUpload\FTPUpload\Form1.resx

     文件       3916  2017-12-15 14:29  FTPUpload\FTPUpload\FTPUpload.csproj

     文件        868  2017-12-15 14:25  FTPUpload\FTPUpload\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       7108  2017-12-15 14:25  FTPUpload\FTPUpload\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件        496  2017-12-15 14:30  FTPUpload\FTPUpload\obj\Debug\FTPUpload.csproj.FileListAbsolute.txt

     文件       1012  2017-12-15 14:29  FTPUpload\FTPUpload\obj\Debug\FTPUpload.csproj.GenerateResource.Cache

     文件       9728  2017-12-15 14:32  FTPUpload\FTPUpload\obj\Debug\FTPUpload.exe

     文件        180  2017-12-15 14:29  FTPUpload\FTPUpload\obj\Debug\FTPUpload.Form1.resources

     文件      19968  2017-12-15 14:32  FTPUpload\FTPUpload\obj\Debug\FTPUpload.pdb

     文件        180  2017-12-15 14:29  FTPUpload\FTPUpload\obj\Debug\FTPUpload.Properties.Resources.resources

     文件          0  2017-12-15 14:25  FTPUpload\FTPUpload\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2017-12-15 14:25  FTPUpload\FTPUpload\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2017-12-15 14:25  FTPUpload\FTPUpload\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件        521  2017-12-15 14:25  FTPUpload\FTPUpload\Program.cs

     文件       1326  2017-12-15 14:25  FTPUpload\FTPUpload\Properties\AssemblyInfo.cs

     文件       2831  2017-12-15 14:25  FTPUpload\FTPUpload\Properties\Resources.Designer.cs

     文件       5612  2017-12-15 14:25  FTPUpload\FTPUpload\Properties\Resources.resx

     文件       1096  2017-12-15 14:25  FTPUpload\FTPUpload\Properties\Settings.Designer.cs

     文件        249  2017-12-15 14:25  FTPUpload\FTPUpload\Properties\Settings.settings

     文件        994  2017-12-15 14:25  FTPUpload\FTPUpload.sln

............此处省略14个文件信息

评论

共有 条评论