【发布时间】:2011-07-30 10:12:05
【问题描述】:
我正在尝试通过拖放将多个文件添加到 FTP 服务器,我可以使用 try catch 块来做到这一点,如果我们正确地提供了 ftp 设置,则上传它们需要 1 秒,但是当我们提供错误的详细信息时挂断并且不给我任何错误消息,但如果我给出一个异常消息。
现在,我添加的每个文件都会收到错误消息和成功消息。我不希望这种情况发生。
谁能告诉我应该在哪里发送成功和失败的消息,以便上传需要几秒钟,如果没有应该立即给我一条消息。
我完全不知道我哪里出错了。
任何帮助将不胜感激!
这是我的代码:
Private Sub uploadFile(ByVal FTPAddress As String, ByVal filePath As String, ByVal username As String, ByVal password As String) 'Create FTP request
Try
Dim request As FtpWebRequest = DirectCast(FtpWebRequest.Create(FTPAddress & "/" & Path.GetFileName(filePath)), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.UploadFile
request.Credentials = New NetworkCredential(username, password)
request.UsePassive = True
request.UseBinary = True
request.KeepAlive = False
Dim buffer As Byte() = Nothing
'Load the file
Using stream As FileStream = File.OpenRead(filePath)
buffer = New Byte(CInt(stream.Length - 1)) {}
stream.Read(buffer, 0, buffer.Length)
End Using
'Upload file
Using reqStream As Stream = request.GetRequestStream()
reqStream.Write(buffer, 0, buffer.Length)
End Using
MsgBox("Uploaded Successfully", MsgBoxStyle.Information)
Catch
MsgBox("Failed to upload.Please check the ftp settings", MsgBoxStyle.Critical)
End Try
End Sub
这是拖放的代码
Private Sub FlowLayoutPanel1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles FlowLayoutPanel1.DragDrop
Try
Dim Files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
For Each FileName As String In Files
Dim Extension As String = Path.GetExtension(FileName).ToLower
If Array.IndexOf(SupportedExtensions, Extension) <> -1 Then
uploadFile(txtFTPAddress.Text, FileName, txtUsername.Text, txtPassword.Text)
End If
Next
Catch
End Try
End Sub
【问题讨论】:
标签: vb.net winforms error-handling ftp