思路:
1、建立server端的上传模块
2、建立WCF服务接口
3、配置web.config文件
4、建立client端的脚本
一、建立server端的上传过程(废话不多说,直接上代码)
Imports System.Net.WebClient
Imports System.IO
Imports System.Net
Public Module ftpHelper
Private ReadOnly SMTPuserid As String = "登录名"
Private ReadOnly SMTPpassword As String = "登录密码"
Public Sub FtpUpload(ByVal filename As String, ByVal byt() As Byte)
Dim fwc As New WebClient
fwc.Credentials = New NetworkCredential(SMTPuserid, SMTPpassword)
Dim add As String = "ftp://bxu2359560474.my3w.com/myfolder/" + filename
fwc.UploadData(add, byt)
End Sub
End Module
二、建立WCF服务接口
选择server点右键->添加->新建项->web->WCF服务,输入名称FtpService,此时会建立两个文件(FtpService,IFtpService)。
Imports System.ServiceModel
Imports System.ServiceModel.Activation
' 注意: 使用上下文菜单上的“重命名”命令可以同时更改代码、svc 和配置文件中的类名“FtpService”。
' 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 FtpService.svc 或 FtpService.svc.vb,然后开始调试。
Namespace LightSwitchApplication
<AspNetCompatibilityRequirements(
RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class FtpService
Implements IFtpService
Public Function FtpUpload(filename As String, byt() As Byte) As Boolean Implements IFtpService.ftpUp
Try
ftpHelper.FtpUpload(filename, byt)
Catch ex As Exception
Return False
End Try
Return True
End Function
End Class
End Namespace
Imports System.ServiceModel
Imports System.ServiceModel.Web
' 注意: 使用上下文菜单上的“重命名”命令可以同时更改代码和配置文件中的接口名“IFtpService”。
<ServiceContract()>
Public Interface IFtpService
<OperationContract()>
<WebInvoke(Method:="POST", BodyStyle:=WebMessageBodyStyle.Wrapped, ResponseFormat:=WebMessageFormat.Xml)>
Function ftpUp(filename As String, byt() As Byte) As Boolean
End Interface
看对应代码就知道是对应哪个文件了。
三、配置web.config文件
打开web.config,找到<system.serviceModel>,拉到最后就能找到
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<behaviors>
<!-- 服务行为 -->
<serviceBehaviors>
<behavior>
<dataContractSerializer maxItemsInObjectGraph="6553600" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding maxReceivedMessageSize="6553600" />
</webHttpBinding>
<basicHttpBinding>
<binding name="FtpServiceBinging" maxReceivedMessageSize="52428800">
<security mode="None">
<transport clientCredentialType="None"/>
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="LightSwitchApplication.FtpService">
<endpoint address="FtpService.svc" bindingConfiguration="FtpServiceBinging" binding="basicHttpBinding" contract="IFtpService" />
</service>
</services>
</system.serviceModel>
这里面我也不是全明白。对应版本是VS2013。照了2015的书试了半天都是错了。上面这个代码是最后修改成功的。最大附件在20M左右。
<binding name="FtpServiceBinging" maxReceivedMessageSize="52428800">
这是控制上传文件大小的。不知道为什么目前只能上传到20M。求高手解答。
四、建立client端的脚本(这个好理解)
Private Sub OpenfileUpload()
Dispatchers.Main.Invoke(
Sub()
Dim openDialog As New OpenFileDialog
openDialog.Multiselect = False
openDialog.Filter = "All File|*.*"
If openDialog.ShowDialog = True Then
If openDialog.File.OpenRead.Length > 20971520 Then
Me.ShowMessageBox("考虑到服务器的存储量,请选择小于20M的文件上传!")
Else
Dim fileData As FileStream = openDialog.File.OpenRead
If fileData.Length > 0 Then
Dim fileBArray(fileData.Length - 1) As Byte
fileData.Read(fileBArray, 0, fileData.Length)
Me.ShowMessageBox(fileData.Length)
fileData.Close()
fileData.Dispose()
Dim ftpupload As New fttp.FtpServiceClient
ftpupload.ftpUpAsync("ddd4.txt", fileBArray)
End If
End If
End If
End Sub)
End Sub