无法通过C#

时间:2017-10-29 10:41:47

标签: c# google-drive-api

在该计划中,我需要下载到Google云端硬盘。我在互联网上找到了一个例子https://www.daimto.com/google-drive-api-c-upload/。我按照示例执行了所有操作,但是出现了这样的问题,代码正常运行,没有产生错误,但在Google云端硬盘上本身没有文件出现。

public static Google.Apis.Drive.v2.Data.File uploadFile(Google.Apis.Drive.v2.DriveService _service, string _uploadFile, string _parent)
        {

            if (System.IO.File.Exists(_uploadFile))
            {
                Google.Apis.Drive.v2.Data.File body = new Google.Apis.Drive.v2.Data.File();
                body.Title = System.IO.Path.GetFileName(_uploadFile);
                body.Description = "File uploaded by .NET";
                body.MimeType = GetMimeType(_uploadFile);
                body.Parents = new List<Google.Apis.Drive.v2.Data.ParentReference>() { new Google.Apis.Drive.v2.Data.ParentReference() { Id = _parent } };

                // File's content.
                byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
                System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
                try
                {
                    Google.Apis.Drive.v2.FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
                    request.Upload();
                    return request.ResponseBody;
                }
                catch (Exception e)
                {
                    MessageBox.Show("An error occurred: " + e.Message);
                    return null;
                }
            }
            else
            {
                MessageBox.Show("File does not exist: " + _uploadFile);
                return null;
            }
        }

找到代码的一部分return request.ResponseBody; 返回Null,但为什么会发生这种情况我无法理解。

0 个答案:

没有答案