C#实现FTP上传资料

C#实现FTP上传资料

1.通过用FTP进行上传文件,首先要实现建立FTP连接,一般建立FTP连接,需要知道FTP配置有关的信息。一般要在Bean中建立一个ServiceFileInfo.cs文件进行记录,一般需要FTP地址、登录用户名和登录密码。然后通过其他页面进行访问读取。代码样式如下:

class ServiceFileInfo    {        // service1        public static string txtFilePath = @"ftp://12.128.128.01/FileName/";        //userid & password        public static string txtUID = "username";        public static string txtPWD = "password";    }

2.通过主方法读取Bean文件下面的的ServiceFileInfo.cs文件的信息,去实现建立FTP连接。这里还需要清楚的知道你上传文件的路径(Path)和文件名称(FileName)。根据这些信息主方法去调用写着Bean中的另外一个ftpOperation.cs 文件(这个.cs文件中主要写一些关于FTP的操作方法),进行FTP访问操作。

  • 主方法调用FTP操作代码
ExecutionResult exeRes = this.ftpOperation.UploadFile(textFilePath, txtUID, txtPWD, Path + "/" + FileName + ".txt");//.txt为文件的后缀名
  • Bean文件中ftpOperation.cs文件关于FTP操作的方法
public ExecutionResult UploadFile(string vIMSPath, string vUID, string vPassword, string vLocalPath)        {            ExecutionResult result = new ExecutionResult();            result = connectState(vIMSPath, vUID, vPassword, vLocalPath);//调用下面代码方法            if (result.Status)            {                File.Delete(vLocalPath);            }            return result;        }

connectState()方法

public static ExecutionResult connectState(string vIMSPath, string vUID, string vPassword, string fileName)        {            string operater = "";            bool Flag = false;            ExecutionResult result;            result = new ExecutionResult();            lock (lockObj)            {                try                {                    operater = "Connet to FTP";                    FTPOperation ftp = new FTPOperation(new Uri(vIMSPath), vUID, vPassword);                    operater = "Upload file";                    Flag = ftp.UploadFile(fileName, Path.GetFileName(fileName), true);                    if (Flag)                    {                        result.Status = true;                        result.Message = "Send to server OK";                    }                }                catch (Exception ex)                {                    result.Status = false;                    result.Anything = "Mail";                    result.Message = operater + ":" + ex.Message;                }            }            return result;        }
  • UploadFile()方法
public bool UploadFile(string LocalFullPath, string RemoteFileName, bool OverWriteRemoteFile)        {            bool result;            try            {                bool flag = !this.IsValidFileChars(RemoteFileName) || !this.IsValidFileChars(Path.GetFileName(LocalFullPath)) || !this.IsValidPathChars(Path.GetDirectoryName(LocalFullPath));                if (flag)                {                    throw new Exception("非法文件名或目录名!");                }                bool flag2 = File.Exists(LocalFullPath);                if (!flag2)                {                    throw new Exception("本地文件不存在!");                }                FileStream fileStream = new FileStream(LocalFullPath, FileMode.Open, FileAccess.Read);                byte[] array = new byte[fileStream.Length];                fileStream.Read(array, 0, (int)fileStream.Length);                fileStream.Close();                result = this.UploadFile(array, RemoteFileName, OverWriteRemoteFile);            }            catch (Exception ex)            {                this.ErrorMsg = ex.ToString();                throw ex;            }            return result;        }public bool UploadFile(byte[] FileBytes, string RemoteFileName)        {            bool flag = !this.IsValidFileChars(RemoteFileName);            if (flag)            {                throw new Exception("非法文件名或目录名!");            }            return this.UploadFile(FileBytes, RemoteFileName, false);        }public bool UploadFile(byte[] FileBytes, string RemoteFileName, bool OverWriteRemoteFile)        {            bool result;            try            {                bool flag = !this.IsValidFileChars(RemoteFileName);                if (flag)                {                    throw new Exception("非法文件名!");                }                bool flag2 = !OverWriteRemoteFile && this.FileExist(RemoteFileName);                if (flag2)                {                    throw new Exception("FTP服务上面已经存在同名文件!");                }                this.Response = this.Open(new Uri(this.Uri.ToString() + RemoteFileName), "STOR");                Stream requestStream = this.Request.GetRequestStream();                MemoryStream memoryStream = new MemoryStream(FileBytes);                byte[] array = new byte[1024];                int num = 0;                for (;;)                {                    int num2 = memoryStream.Read(array, 0, array.Length);                    bool flag3 = num2 == 0;                    if (flag3)                    {                        break;                    }                    num += num2;                    requestStream.Write(array, 0, num2);                }                requestStream.Close();                this.Response = (FtpWebResponse)this.Request.GetResponse();                memoryStream.Close();                memoryStream.Dispose();                FileBytes = null;                result = true;            }            catch (Exception ex)            {                this.ErrorMsg = ex.ToString();                throw ex;            }            return result;        }
(0)

相关推荐

  • 【C#】工具类-FTP操作封装类FTPHelper

    C# FTPHelper实现FTP服务器文件读写操作,支持SSL协议(FTP服务器为:Serv-U10.0). using System;using System.Collections.Generi ...

  • C# FileStream类

    C# FileStream类 在 C# 语言中文件读写流使用 FileStream 类来表示,FileStream 类主要用于文件的读写,不仅能读写普通的文本文件,还可以读取图像文件.声音文件等不同格 ...

  • JAVA技术实现上传下载文件到FTP服务器(完整)

    FavFTPUtil.Java package com.favccxx.favsoft.util; import java.io.File; import java.io.FileInputStrea ...

  • 百度文库也能赚钱啦!业余时间上传资料也能赚外快,这份行动指南请收好!

    百度文库也能够赚钱? 当然,在内容创业的时代,只要该平台有流量,只要你会创作,那么你就能够赚钱! 说起百度文库,大家应该并不陌生.尤其是那些经常需要下载资料的小伙伴. 百度文库是一款网友在线分享文档的 ...

  • godaddy空间FTP上传网页文件与网站程序​

    godaddy空间FTP上传网页文件与网站程序 1.下载cuteftp软件,安装 2.新建ftp站点. 如果是GoDaddy的用户,ftp帐号密码就是你开通空间的时候设置的,查看空间ip教程:http ...

  • 如何使用cmd命令行登录ftp上传下载文件

    如何使用cmd命令行登录ftp上传下载文件. 视频教程: cmd命令提示符,常见ftp中命令. 进入ftp后的命令有: get 下载, put 上传, cd ftp目录, lcd 本地目录. mget ...

  • FTP上传网页注意事项

    2017-06-07 13:26:11   注意事项: 以下文件和文件夹均可直接删除,如需用到文件夹需要的功能,新建同名的目录即可. Linux 主机上传网页文件时应注意以下几点:  请您将网页上传到 ...

  • 老师自己创作的教案、课件、计划等资料,在这里上传,有人下载就能赚钱

    但文件下载限速一直是一个老生常谈的问题.动辄几十KB/s,甚至几KB/s,慢的让人痛不欲生.很多时候还会遇到会员制.空间小.操作复杂等问题. 所以,今天给大家带来一款好用到飞起的网盘工具--夹子盘,不 ...

  • 发纯生信SCI,期刊主编一定让我上传伦理资料,该如何回复?

    一般做纯生信数据挖掘,基本离不开这两个数据库:TCGA.GEO.按道理,涉及到人或者动物的研究都要上传伦理资料的,但是做纯生信数据挖掘,病人数据都是从数据库上下载的,没有涉及到伦理问题,故不用上传伦理 ...

  • 为什么优酷上传视频失败

    优酷app上传视频经历过好多次失败,总是说视频文件不存在.终于发现解决办法:失败的视频别删除,关闭app,过段时间再打开app,视频会显示正在上传.如果上传进度卡住了,可以点暂停,然后继续会好一点.或 ...

  • 优酷上传视频,保存失败

    今天突然遇到了上传视频 保存时不断地出现错误,试了好多次,好多办法,都不行,网上搜了好几遍,很多人都没弄出所以然来,弄了半天终于知道原因了,并且完美解决.给遇到同样问题的好友们指明一下方向,希望他们在 ...