我的客户有一个要求。
他希望通过点击按钮上传文件夹“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 Control吗?
如果是,那么如何选择要从中上传文件的特定文件夹?
或者我应该如何以编程方式将Filename提供给FileUpload Control?因为它是只读的。
答案 0 :(得分:2)
你真的让你的服务器只是合法地从客户端机器上获取文件。这是文件上传控件的用途。通常,客户端会启动上载。
您可以使用具有多个属性的文件上传控件,以允许您在IE 10+,FF,Chrome,Opera中选择多个文件。否则,如果没有multiple属性,则上传文件会很繁琐,因为您无法切换/控制单击所有文件。
您将文件保存在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);
}
}
在制作中,这需要您的服务器拥有文件夹权限。