博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vb.net上传文件到FTP服务器
阅读量:4662 次
发布时间:2019-06-09

本文共 2790 字,大约阅读时间需要 9 分钟。

上传文件到ftp服务器整理:

  声明:以下代码为vb.net代码,使用Visual Studio 2008开发,所有代码均通过测试,可以直接使用。欢迎转载,请注明出处,欢迎拍砖。

web.config 文件配置, 将用户名,密码和ftp服务器的地址存放到web.config文件:

文件上传步骤:

  1.读取文件到一个FileStream

  2.设置ftp服务器访问信息,主要是ftp服务器地址,访问ftp服务器的用户名和密码,访问类型,等待时间等待。

  3.在ftp服务器中创建一个空的文件

  3.将源文件读取到byte()数组中

  4.将byte()数组中存放的数据写入到另外一个Stream, 这个stream是我们刚在ftp服务器上创建的空文件

  5.完成写入

以下是文件上传代码:

''' ''' upload txt file to specified destination. ''' '''
Private Sub uploadFileByFTP() 'is uploaded Dim isUploaded As Boolean = False 'File path Dim fi As New FileInfo(Server.MapPath(".").ToString & "\TestFileFolder\" & strFileName) 'Create a file in the ftp file server Dim reqObj As System.Net.FtpWebRequest reqObj = DirectCast(System.Net.WebRequest.Create(ConfigurationManager.AppSettings("CompleteFTPPath") & fi.Name), System.Net.FtpWebRequest) 'Configure the request methods as uploadfile With reqObj .Credentials = New Net.NetworkCredential(ConfigurationManager.AppSettings("UserName"), ConfigurationManager.AppSettings("PassWord")) .Method = WebRequestMethods.Ftp.UploadFile .UseBinary = True .ContentLength = fi.Length .Timeout = 10 * 1000 End With Try Using fsSource As FileStream = New FileStream(Server.MapPath(".").ToString & "\NameplateFile\" & strFileName, FileMode.Open, FileAccess.Read) ' Read the source file into a byte array. Dim bytes() As Byte = New Byte((fsSource.Length) - 1) {} ' Get the length of the source file Dim numBytesToRead As Integer = CType(fsSource.Length, Integer) Dim numBytesRead As Integer = 0 While (numBytesToRead > 0) ' Read may return anything from 0 to numBytesToRead. Dim n As Integer = fsSource.Read(bytes, numBytesRead, _ numBytesToRead) ' Break when the end of the file is reached. If (n = 0) Then Exit While End If numBytesRead = (numBytesRead + n) numBytesToRead = (numBytesToRead - n) End While numBytesToRead = bytes.Length ' Write the byte array to the ftp file server Using fsNew As System.IO.Stream = reqObj.GetRequestStream() fsNew.Write(bytes, 0, numBytesToRead) isUploaded = True End Using End Using       ' upload successfully        File.Delete(Server.MapPath(".").ToString & "\TestFileFolder\" & strFileName) If isUploaded = True Then lblMsg.Text = "File uploaded successfully!" Else lblMsg.Text = "" End If Catch ex As Exception Throw ex End Try End Sub

以上是一个简单的上传文件的代码,仅供参考。

相关链接:

create a new FTP site

转载于:https://www.cnblogs.com/Dannier/archive/2011/09/21/ftp.html

你可能感兴趣的文章
《DSP using MATLAB》Problem 5.31
查看>>
HttpWebRequest解析 作用 介绍
查看>>
阿里云 centos vim 显示中文 乱码
查看>>
《DSP using MATLAB》示例Example7.24
查看>>
第一阶段——站立会议总结DAY01
查看>>
结合使用 Oracle Database 11g 和 Python
查看>>
C C++ 去除 unused的提示
查看>>
STL Algorithms 之 unique
查看>>
树边,前向边,后向边,横叉边
查看>>
POJ - 2115 C Looooops(扩展欧几里德求解模线性方程(线性同余方程))
查看>>
山东省第三届ACM省赛
查看>>
第53题_Maximum_Subarray
查看>>
JS作用域题
查看>>
【NOIP2016】天天爱跑步
查看>>
jQuery基础语法使用
查看>>
03第八届蓝桥杯省赛真题- 2.等差素数列
查看>>
【Albert带你1小时看遍美国前沿科技与商业运作】微访谈精选
查看>>
管理系统
查看>>
jqurey 遍历 div内的所有input单选复选按钮并判断是否选中及Attr(checked)无效的解决...
查看>>
export命令import命令
查看>>