0

我的客户有一个要求。

他想通过单击按钮上传文件夹“D:/MyFolder/”中的所有文本文件。

所以我尝试了它,但我遇到了问题。

这是代码:

Protected Sub btnAutoUpload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAutoUpload.Click

    Dim dbProvider As String
    Dim dbSource As String
    Dim con As New OleDbConnection

    Dim SQL As String
    Dim da As OleDbDataAdapter
    Dim ds As New DataSet

    'Connecting and retrieving data form Database.
    dbProvider = "PROVIDER = Microsoft.Ace.OLEDB.12.0;"
    dbSource = "Data Source = '" & Server.MapPath("~/App_Data/Data.accdb") & "'"

    con.ConnectionString = dbProvider & dbSource

    SQL = "SELECT * FROM tblUserDetails WHERE UserType = 'Company'"
    da = New OleDbDataAdapter(SQL, con)
    ds.Clear()
    da.Fill(ds, "tblUserDetails")

    Dim fileUploadPath As String = "C:/MUNIM/"

    Dim NumberOfFilesToUpload As Integer = FileCount(fileUploadPath)

    Dim Extension As String
    Dim fileName As String

    Dim files() As String
    files = IO.Directory.GetFiles(fileUploadPath)

    For x As Integer = 0 To NumberOfFilesToUpload - 1

        Extension = IO.Path.GetExtension(files(x))

        If Extension = ".txt" Then

            fileName = IO.Path.GetFileNameWithoutExtension(files(x))

            If fileName = ds.Tables("tblUserDetails").Rows(x).Item("FileName") Then

                'Count the no. of document uploaders
                Dim lineCount = IO.File.ReadAllLines(documentViewersPath).Length

                'reading the names of document uploaders
                Dim reader As New System.IO.StreamReader(documentViewersPath)
                Dim allLines As New List(Of String)
                Do While Not reader.EndOfStream
                    allLines.Add(reader.ReadLine())
                Loop
                reader.Close()

                For i As Integer = 0 To lineCount - 1

                    If ds.Tables("tblUserDetails").Rows(x).Item("UserName") = ReadLine(i, allLines) Then

                        'Save the file to desired location (Upload File)

                    End If

                Next

            End If

        End If

    Next

End Sub

这怎么可能?

我应该使用 FileUpload 控件吗?

如果是,那么如何选择特定文件夹来上传文件?

或者我应该如何以编程方式将文件名提供给 FileUpload 控件?因为它是只读的。

4

1 回答 1

1

你真的不能让你的服务器合法地从客户端机器上抓取文件。这就是文件上传控件的用途。一般是客户端发起上传。

您可以使用具有多个属性的文件上传控件来允许您在 IE 版本 10+、FF、Chrome、Opera 中选择多个文件。否则,如果没有多重属性,上传文件会很乏味,因为您无法移动/控制单击所有文件。

您将文件保存在httpfilecollection中,然后简单地循环并使用 .SaveAs 属性保存到目标文件。如果您需要,我可以提供相同的代码..

Asp

<asp:FileUpload ID="FileUpload1" multiple runat="server" />


<br />

<asp:Button ID="Button1" runat="server" Text="Upload" onclick="Button1_Click" 
    Height="26px" />

c#你或其他人请转换,我很久没做过 vb 了:P

protected void Button1_Click(object sender, EventArgs e)
{
    HttpFileCollection hfc = Request.Files;
    for (int i = 0; i != hfc.Count; i++ )
    {
        hfc[i].SaveAs(@"D:\MyFolder\" + hfc[i].FileName);
    }

}

在生产中,这将要求您的服务器具有文件夹权限。

于 2013-05-12T04:14:12.870 回答