这是我的代码。
是的,我正在使用DropNet和Dropbox API,因为我发现DropNet上传效果很好。但我正在尝试使用Dropbox来检查文件名(因为我无法在DropNet上使用它并且无法在线找到任何帮助)
我毫不怀疑我的问题与整个Async& amp;等待,因为我之前从未使用过这些东西。
文件上传&得到分享都很好。
这是一个VB.Net网站。 当我运行它时,它会在DoesDropBoxFileExist函数
旁边冻结Imports Dropbox.Api
Imports DropNet
Imports DropNet.Models
Partial Class _Default
Inherits System.Web.UI.Page
Dim br As String = "<br>"
Public FileName As String
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If FileUpload1.HasFile Then
Dim dropNet_client As New DropNetClient("", "", "")
Dim dropBox_client As New DropboxClient("")
FileName = FileUpload1.PostedFile.FileName
Response.Write("before: " & FileName & br)
MsgBox(1)
FileName = DoesDropBoxFileExist(dropBox_client).Result
MsgBox(3)
Response.Write("after: " & FileName & br)
Dim content As Byte() = FileUpload1.FileBytes
Dim pathToFile As String = Server.MapPath("~")
'Response.Write(pathToFile)
dropNet_client.UploadFile("/AlertImages/", FileName, content, True)
Dim shareResponse As ShareResponse = dropNet_client.GetShare("/AlertImages/" & FileName)
Response.Write(shareResponse.Url)
If Not FileName.ToLower.Contains("pdf") Then
Dim rawBytes As Byte() = dropNet_client.GetThumbnail("/AlertImages/" & FileName, 2)
Dim base64String As String = Convert.ToBase64String(rawBytes, 0, rawBytes.Length)
Image1.ImageUrl = "data:image/png;base64," & base64String
Image1.Visible = True
End If
dropBox_client.Dispose()
End If
End Sub
Private Async Function DoesDropBoxFileExist(_client As DropboxClient) As Threading.Tasks.Task(Of String)
Dim rtn As String = FileName
Dim list = Await _client.Files.ListFolderAsync("/AlertImages")
MsgBox(2)
' show folders then files
For Each item As Files.Metadata In list.Entries.Where(Function(i) i.IsFolder)
If item.Name = FileName Then
FileName = FileName & Now.ToString
End If
Response.Write(" < b > " & item.Name & "</b>" & br)
'Dim list2 As ListFolderResult = Await dbx.Files.ListFolderAsync(item.Name)
'For Each itm As Files.Metadata In list2.Entries.Where(Function(j) j.IsFile)
' Response.Write(item.Name & " : " & item.AsFile.Size & br)
'Next
Next
For Each item As Files.Metadata In list.Entries.Where(Function(i) i.IsFile)
Response.Write("'" & item.Name & "' '" & FileName & "'" & br)
If item.Name = FileName Then
Response.Write("test" & br)
rtn = FileName & "_" & Now.ToString
End If
Next
Return rtn
End Function
End Class
答案 0 :(得分:0)
方法1
要使用API在VB.NET中检查文件是否存在于Dropbox中,可以使用此方法。
首先,我们创建一个带有点击事件的按钮,如下所示:
Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
'FileToCheck is declared in Form1 as Public Shared
'FileFound is declared in Form1 as Public Shared
FileToCheck = cmbFiles.Text
FileFound = False
Dim task1 = Task.Run(Function() CheckFileMetadata())
task1.Wait()
If FileFound = True Then
'Do something
Else
'Do something else
End If
End Sub
现在是功能:
Private Async Function CheckFileMetadata() As Task
Using dbx = New DropboxClient(DbxToken) 'DbxToken = your token text
Try
Await dbx.Files.GetMetadataAsync(Form1.FileToCheck)
FileFound = True
Debug.WriteLine("Found it!")
Catch exapi As ApiException(Of Dropbox.Api.Files.GetMetadataError)
If exapi.ErrorResponse.IsPath And exapi.ErrorResponse.AsPath.Value.IsNotFound Then
Debug.WriteLine("Nothing found at " + Form1.FileToCheck)
End If
Catch ex As Exception
Debug.WriteLine("Error checking file metadata" + vbCrLf + ex.ToString)
End Try
End Using
End Function
此方法改编自代码here。
方法2
此示例演示了如何使用VB.NET递归遍历所有Dropbox文件夹以检索所有文件的名称并将它们放入集合中。然后我们检查文件是否在集合中。此方法确实有效,但是由于明显的原因,它不如上述方法有效。我将其保留在此处是因为它说明了一些可能对某人有帮助的其他方法。
一些注意事项:
Public DbxFolders As New List(Of String)
Public DbxFiles As New List(Of String)
Private Sub btnWalk_Click(sender As Object, e As EventArgs) Handles btnWalk.Click
DbxFolders.Clear()
DbxFiles.Clear()
Dim FindIt As String = "/Folder/File-To-Find.txt".ToLower
Dim task2 = Task.Run(Function() GetTree(String.Empty))
task2.Wait()
If DBFileExists(FindIt) Then MsgBox("Found it!") Else MsgBox("File not found")
End Sub
Private Async Function GetTree(dir As String) As Task
Using dbx = New DropboxClient("Your_Token_Goes_Here")
Dim list = Await dbx.Files.ListFolderAsync(dir)
For Each item In list.Entries.Where(Function(i) i.IsFile)
DbxFiles.Add(item.PathLower)
Next
For Each item In list.Entries.Where(Function(i) i.IsFolder)
DbxFolders.Add(item.PathLower)
Await GetTree(item.PathLower)
Next
End Using
End Function
Private Function DBFileExists(file As String) As Boolean
If DbxFiles.IndexOf(file) > -1 Then Return True Else Return False
End Function
讨论
到目前为止,方法1显然是这两种方法中效率最高的一种,因为我们只调用一次API。请注意,如何在Try-Catch中使用ApiException来确定未找到文件。
方法2展示了一些对我有所帮助的其他概念,因此我将其保留在这里,因为有人可能会遇到这种情况,该代码及其创建的列表会派上用场。
请注意,当我们调用GetTree(String.Empty)时,传递特定的文件夹以查找而不是从根目录开始会更有效,因为我们试图匹配完整路径(/ path / to /file.txt),但我想说明递归迭代,因为在不同情况下可能需要它。
如果您不在乎某个项目位于哪个文件夹中,而只想查看它是否存在于某个文件夹中,而不管该位于哪个文件夹中,那么您将需要使用此递归迭代而不是item.pathlower您可能想收集item.name。
如果需要,您可以通过简单的循环处理方法2中收集的文件列表:
For each DbxFile as string in DbxFiles
'Do something
Next