bonny
bonny

Reputation: 738

How to upload file on Google Drive using vb.net?


I need help uploading the file to Google Drive.
Everything works fine but with an error during first trial.
During the debug at request.upload the cursor don't wait (wait for the file to upload) and skips to the next line Dim responsefile As New Data.File and I get nothing in request.ResponseBody.
After that I run the function cursor actually waits on request.upload and it uploads file successfully.
I don't know what is actually happening. I checked the data every time and it is the same.

Public Async Function UploadFile3(service As DriveService, FilePath As String) As Tasks.Task(Of Data.File)

        If service3.ApplicationName <> "netGDriveApi" Then CreateService()
            If IO.File.Exists(FilePath) Then
                Dim body As New Data.File()
                body.Name = IO.Path.GetFileName(FilePath)
                body.Description = "BackUP file"
                body.MimeType = "application/octet-stream"
                'body.FileExtension = ".bak"



                '-------------------------------------------------UPLOAD FILE PROCESS-------------------------------------------------------------

                Dim byteArray As Byte() = IO.File.ReadAllBytes(FilePath)
                Dim stream As New IO.MemoryStream(byteArray)
                Try
                    Dim request As FilesResource.CreateMediaUpload = service.Files.Create(body, stream, body.MimeType)
                    Await request.UploadAsync() 'Cursor skips first time here and dont wait for response.
                    Dim responsefile As New Data.File 'Cursor waits from the above step to here till the file uploaded.


                    responsefile = request.ResponseBody

                    If IsNothing(responsefile) Then
                        MessageBox.Show("Try Again")
                    Else
                        MessageBox.Show(responsefile.Id.ToString)
                    End If

                Catch e As Exception
                    MessageBox.Show("An error occurred: " + e.Message)
                    Return Nothing
                End Try

            Else
                MessageBox.Show("FILE DOES NOT EXISTS." + FilePath)
                Return Nothing
            End If
End Function

Upvotes: 1

Views: 785

Answers (2)

bonny
bonny

Reputation: 738

The line Dim request As FilesResource.CreateMediaUpload = service.Files.Create(body, stream, body.MimeType). Problem is here.
Thanks to @stevec to guide me the error.
The drive service is service3 and in the above line its just written service.Files.Create.
So the actual rectified answer is
Dim request As FilesResource.CreateMediaUpload = service3.Files.Create(body, stream, body.MimeType)

Upvotes: 0

stevec
stevec

Reputation: 62

request.UploadAsync() returns a Task(Of IUploadProgress). At the very least, you should be examining the result of this task. It might give you clues as to the cause of your problems.

For example, while debugging you could do something like the following:

Try
    Dim request As FilesResource.CreateMediaUpload = service.Files.Create(body, stream, body.MimeType)

    Dim Upload As IUploadProgress = Await request.UploadAsync() 'Cursor skips first time here and dont wait for response.

    If Upload.Status <> UploadStatus.Completed Then
        Dim ex As Exception = Upload.Exception
        MessageBox.Show(ex.Message, "UploadAsync Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Return Nothing
    Else
        MessageBox.Show(Upload.Status.ToString, "Upload Status:")
    End If

    Dim responsefile As New Data.File 'Cursor waits from the above step to here till the file uploaded.
    responsefile = request.ResponseBody

Always check what information methods return, and use that information as necessary in your production code.

Upvotes: 2

Related Questions