如何使用一个连接在VB.NET中上传多个文件 [英] How can I use one connection to upload multiple files in VB.NET

查看:71
本文介绍了如何使用一个连接在VB.NET中上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个应用程序从文件夹中读取多个文件并将它们上传到服务器..它工作正常但是当它想要上传每个文件时,它会创建一个连接,上传文件并关闭连接。它对所有文件一遍又一遍地做同样的事情。我想要一种情况,它只创建一个连接,上传所有文件并关闭连接。请任何帮助将不胜感激,我到处搜索,但找不到任何帮助。下面是我的代码





i've this application that reads multiple files from a folder and upload them to the server..it works fine but when it wants to upload each file, it creates a connection, upload the file and closes the connection. it does the same thing for all the files over and over again. i want a situation where it creates just one connection, upload all the files and close the connection. please any help will be appreciated, i searched everywhere but couldn't find any help. below is my code


Imports System.IO
Imports System.Net

Public Class DemoFTPServerApp

Dim _Filename As String
Dim _UploadPath As String
Dim f As String


Public Sub LoadFiles(_User As String, _Password As String, _Path As String)
    Dim _MyArraylist As New ArrayList
    Dim FolderPath As String = "C:\Users\Desktop\files"



    Dim finfo As New DirectoryInfo(FolderPath)
    For Each fi In finfo.GetFiles("*.txt")
        _MyArraylist.Add(fi.FullName) 'full path only
        _Filename = fi.FullName
        f = fi.ToString()
        _UploadPath = _Path & f


       Try
      Dim request As FtpWebRequest = DirectCast(WebRequest.Create(New Uri(_UploadPath)), FtpWebRequest)
            request.Method = WebRequestMethods.Ftp.UploadFile
            request.Credentials = New NetworkCredential(_User, _Password)
            request.UseBinary = True
            request.UsePassive = False
            request.KeepAlive = True
            request.ConnectionGroupName = "company name"
            request.ServicePoint.ConnectionLimit = 4
            request.ServicePoint.CloseConnectionGroup("company name")


            Dim buffer(1023) As Byte
            Dim bytesIn As Long = 1

            Dim filepath As System.IO.FileInfo = New System.IO.FileInfo(_Filename)
            Dim _FileStream As System.IO.FileStream = filepath.OpenRead()
            Dim _Stream As System.IO.Stream = request.GetRequestStream


            Do Until bytesIn < 1
                bytesIn = _FileStream.Read(buffer, 0, 1024)
                If bytesIn > 0 Then
                    _Stream.Write(buffer, 0, bytesIn)

                End If
            Loop

            _Stream.Close()
            _Stream.Dispose()
            _FileStream.Close()
            _FileStream.Dispose()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    Next

    MessageBox.Show("File Succesfully uploaded!")

End Sub

Private Sub btnUploadFile_Click(sender As Object, e As EventArgs) Handles btnUploadFile.Click

   LoadFiles("username", "password", "ftp://ftpsite.com/")

End Sub





我的尝试:



此代码工作正常但我需要一些帮助来修改它。

i尝试将FTP连接从循环中取出但是它会抛出我预期的ArgumentNullException错误。



What I have tried:

This code is working fine but i need a little help on how to modify it.
i tried to take the FTP connection out of the loop but it throws an ArgumentNullException error which i expected.

推荐答案

在考虑任何专门的con之前trols,考虑最简单的可能性:一个Web表单和输入的多个属性控件的类型file 。请参阅:

使用来自Web应用程序的文件

它的外观如下:

Before considering any specialized controls, consider the simplest possibility: a Web form and the multiple attribute of the input control of the type "file". Please see:
Using files from web applications.
This is how it may look:
<form action="someScript.asp">
  <!-- ... -->
  Select images: <input type="file" name="fileSet" multiple="true">
  <input type="submit">
</form>



此表格会向服务器端发送HTTP请求;输入所有文件。属性 multiple 的实际效果是文件对话框将以多选模式显示,因此用户可以选择多个文件。所有文件数据都将在HTTP请求中发送到服务器端,您可以在其中处理它。

另请参阅:

HTML表单指南 - Web开发人员指南

<形式> - HTML



-SA


Such form will send HTTP request to the server side; with all the files entered. The real effect of the attribute multiple is that the file dialog box will be shown in multi-select mode, so the user can chose more than one file. All the file data will be sent in the HTTP request to the server side where you can handle it.
See also:
HTML forms guide — Web developer guides,
<form> — HTML.

—SA


这篇关于如何使用一个连接在VB.NET中上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆