【问题标题】:Google Picker => How to download the fileGoogle Picker => 如何下载文件
【发布时间】:2017-07-24 02:59:05
【问题描述】:

如果重要的话,这里是 C#。我想做的是允许手机(或台式机)上的用户将文件上传到我的网络服务器。我已经构建了选择器,为范围 drive.readonly 找到了所有的身份验证。

但是当我下载文件时,我在示例中看到的是他们使用 Javascript 下载它,然后再次将其上传到服务器。我想从我的服务器上获取它。我在 Dropbox 上可以正常工作,但谷歌是个问题。

我按照这里的指南进行操作: https://developers.google.com/picker/docs/

然后我将 fileId 和令牌发送到服务器:

 function pickerCallback(data) {
    if (data.action == google.picker.Action.PICKED) {
        var datastring = "url=" + data.docs[0].url + "&name=" + data.docs[0].name + "&mimetype=" + data.docs[0].mimetype + "&fileId=" + data.docs[0].id + "&token=" + oauthToken;
            $.ajax({
                type: "POST",
                url: "/FileUpload/GetGoogleDriveFile/",
                data: datastring,
                dataType: "json",
                success: function (data) {
                    if (data.success != null && data.success == true) {
                        window.location.href = "@Html.Raw(AfterUploadDestination)";
                    }
                    else
                    {
                        alert("Error: " + data.ErrorMsg);
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    console.log(xhr.responseText);
                }
            });


        googlepicker.setVisible(false);
    }
}

然后在服务器上我有类似...

 var client = new System.Net.WebClient();
            client.Headers.Set(HttpRequestHeader.Authorization, "Bearer " + file.token);

然后是消耗文件的东西:

doc.Contents = client.DownloadData(string.Format("https://www.googleapis.com/drive/v3/files/{0}?alt=media", file.fileId));

问题在于该文件不仅仅是文件的二进制流,而是当您使用该链接访问网站时 google 显示的网页本身。

我到底要怎么下载文件?

提前致谢,我希望这篇文章不会太长以至于没有人阅读。

【问题讨论】:

  • 也许我需要采用与此类似的解决方案:stackoverflow.com/a/29290636/881954 在服务器端重新进行身份验证。但是,如果用户在批准我的应用并选择了一个文件后又收到另一个弹出窗口,那真是太麻烦了。

标签: javascript c#-4.0 google-picker


【解决方案1】:

您可以查看documentation,了解如何使用 Google Picker API 打开文件。当用户从列表中选择一个文件时,file ID 被返回,ID 可能被您的应用程序用来访问该文件。在打开文件时从选择器中获取file ID 后,应用程序可以获取文件元数据并下载文件内容,如files.get 的参考文档中所述。

【讨论】:

【解决方案2】:

是我。

我问 Google 可能是什么问题,并且支持回来了,您需要创建一个类型为“其他”的客户端 ID。所以当它不起作用时,我认为这是我的代码。

我需要做的就是使用提供的客户端 ID 作为网站(Web 应用程序的类型)并创建一个特定的 API 密钥供选择器使用。

对于任何有问题的人,这是代码的核心:

所以我的客户端代码是:

function pickerCallback(data) {
        if (data.action == google.picker.Action.PICKED) {
            var datastring = "url=" + data.docs[0].url + "&name=" + data.docs[0].name + "&mimetype=" + data.docs[0].mimetype + "&fileId=" + data.docs[0].id + "&token=" + oauthToken;
                $.ajax({
                    type: "POST",
                    url: "/FileUpload/GetGoogleDriveFile/",
                    data: datastring,
                    dataType: "json",
                    success: function (data) {
                        if (data.success != null && data.success == true) {
                            window.location.href = "@Html.Raw(AfterUploadDestination)";
                        }
                        else
                        {
                            alert("Error: " + data.ErrorMsg);
                        }
                    },
                    error: function (xhr, textStatus, errorThrown) {
                        console.log(xhr.responseText);
                    }
                });
            googlepicker.setVisible(false);
        }
    }

服务器代码是:

 public class GoogleFileDetails
        {
            public string url { get; set; }
            public string name { get; set; }
            public int mimetype { get; set; }
            public string fileId { get; set; }
            public string token { get; set; }
        }

[HttpPost]
 public ActionResult GetGoogleDriveFile(GoogleFileDetails file) {

            byte[] contents = null;
            try
            {
                var client = new System.Net.WebClient();
                client.Headers.Set(HttpRequestHeader.Authorization, "Bearer " + file.token);
                contents = client.DownloadData(string.Format("https://www.googleapis.com/drive/v2/files/{0}?alt=media", file.fileId));
            } catch etc...

【讨论】:

    【解决方案3】:

    我使用此代码通过谷歌选择器从驱动器下载二进制图像。技巧部分在 xhr.responseType = 'blob';

    var xhr = new XMLHttpRequest();
    xhr.open('GET', THE_PREVIOUSLY_GOTTEN_DOWNLOAD_URL);
    xhr.setRequestHeader('Authorization', 'Bearer ' + THE_OBTAINED_OAUTHTOKEN);
    xhr.responseType = 'blob';
    
    xhr.onload = function () {
    
       // Better trigger and event and manage the response outside the onload function. 
       // Just for demo purposes:
    
       $('#THE_IMG_ID').attr( 'src', URL.createObjectURL(xhr.response) );
    };
    
    xhr.onerror = function () {
    };
    
    xhr.send();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 1970-01-01
      相关资源
      最近更新 更多