当我尝试从Google云端硬盘帐户下载pdf文件时,出现500错误,当尝试下载docx文件时,该文件正在下载但出现损坏的文件。
这是我的代码:
<script type="text/javascript">
// The Browser API key obtained from the Google API Console.
// Replace with your own Browser API key, or your own key.
var developerKey = 'MY_API_KEY';
// The Client ID obtained from the Google API Console. Replace with your own Client ID.
var clientId = "MY_CLIENT_ID.apps.googleusercontent.com"
// Replace with your own project number from console.developers.google.com.
// See "Project number" under "IAM & Admin" > "Settings"
var appId = "MY_APP_ID";
// Scope to use to access user's Drive items.
var scope = ['https://www.googleapis.com/auth/drive'];
var pickerApiLoaded = false;
var oauthToken;
// Use the Google API Loader script to load the google.picker script.
function loadPicker() {
gapi.load('auth', {'callback': onAuthApiLoad});
gapi.load('picker', {'callback': onPickerApiLoad});
}
function onAuthApiLoad() {
window.gapi.auth.authorize(
{
'client_id': clientId,
'scope': scope,
'immediate': false
},
handleAuthResult);
}
function onPickerApiLoad() {
pickerApiLoaded = true;
createPicker();
}
function handleAuthResult(authResult) {
if (authResult && !authResult.error) {
oauthToken = authResult.access_token;
createPicker();
}
}
// Create and render a Picker object for searching images.
function createPicker() {
if (pickerApiLoaded && oauthToken) {
var view = new google.picker.View(google.picker.ViewId.PDFS);
view.setMimeTypes("application/pdf");
var picker = new google.picker.PickerBuilder()
.enableFeature(google.picker.Feature.NAV_HIDDEN)
.enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
.setAppId(appId)
.setOAuthToken(oauthToken)
.addView(view)
.addView(new google.picker.DocsUploadView())
.setDeveloperKey(developerKey)
.setCallback(pickerCallback)
.build();
picker.setVisible(true);
}
}
// A simple callback implementation.
function pickerCallback(data) {
if (data.action == google.picker.Action.PICKED) {
var fileId = data.docs[0].id;
var url= data.docs[0].webContentLink;
var filename = data.docs[0].originalFilename;
$.ajax({
type: "POST",
url: "test.aspx/GetDriveFile",
data: JSON.stringify({
FileName: filename, UploadUrl: url
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
if (msg.d == "True") {
alert("Success");
}
},
error: function (msg) {
alert("Failed");
});
}
}
</script>
<script src="https://www.google.com/jsapi?key=MY_API_KEY"></script>
<script src="https://apis.google.com/js/client.js?onload=loadPicker"></script>
如果将选择器视图设置为以下显示的类型,则可以下载docx文件,但该文件已损坏。
var view = new google.picker.View(google.picker.ViewId.DOCUMENTS);
这是我的后端代码:
<WebMethod()>
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Shared Function GetDriveFile(ByVal FileName As String, ByVal UploadUrl As String) As String
'https://drive.google.com/uc?export=download&id=0B0gjYWPmUtL7cDFQZG9hZWlnaW8
Try
Dim UploadedPath As String = HttpContext.Current.Server.MapPath("UploadedFiles/") + FileName
Dim Client As New WebClient()
Client.DownloadFile(UploadUrl, UploadedPath)
Return "True"
Catch ex As Exception
Return ex.Message
End Try
End Function