我正在尝试将pdf文件从google drive上传到s3服务器,但是没有正确上传。
我使用谷歌驱动器选择器获取文件 这是我的
以下是我的google驱动器选择器代码: - (客户端代码)
UIViewController
在这里我可以获取文件网址
喜欢: https://drive.google.com/file/d/0B39VaBFZeygDNEVuWDYtYlgxNGhTeVdKNWtSX0tpT0FzeDRB/view?usp=drive_web
这是我的服务器端代码
s3upload_rou.js文件(路由器文件)
<script type="text/javascript" src="https://apis.google.com/js/api.js"></script>
// The Browser API key obtained from the Google Developers Console.
// Replace with your own Browser API key, or your own key.
var developerKey = 'XXXXXXXXXX_ff_NX66eb-XXXXXXXXXXX';
// The Client ID obtained from the Google Developers Console. Replace with your own Client ID.
var clientId = "XXXXXXXXXX-hs4ujaro5sc3d0g8qndtcq2tl279cfm1.apps.googleusercontent.com"
// Replace with your own App ID. (Its the first number in your Client ID)
var appId = "XXXXXXXXXX";
// Scope to use to access user's Drive items.
var scope = ['https://www.googleapis.com/auth/drive'];
var pickerApiLoaded = false;
var oauthToken;
$scope.addGoogleDriveFile = function() {
if (!isValidSelectedCategories(getSelectedCategories())) {
return;
}
if (!isValidSelectedCategoriesNoapost(getSelectedCategories())) {
return;
}
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.DOCS);
var picker = new google.picker.PickerBuilder()
.enableFeature(google.picker.Feature.NAV_HIDDEN)
.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) {
console.log(data);
addGoogleDrivePDF (data.docs);
}
}
var addGoogleDrivePDF = function (file) {
var pdfData = {
url: file[0].url
};
$http.post('/uploadto/s3/drtivepdf', pdfData )
.success((function (article) {
console.log(article);
}).error(function (error) {
console.log(article);
});
}
s3upload_ctrl.js文件(控制器文件)
app.post('/uploadto/s3/drtivepdf', function (req, res, next) {
next();
}, bookmark.addGoogleDrivePDF );
这是我的file.js代码: - '
var async = require('async');
var config = require('../config');
var file = require('../libs/file');
exports.addDropboxBookmark = function (req, res, next) {
saveGoogleDrivePdf(req.body.url, function (err, pdfDetail) {
if (err) {
cb(err);
} else {
res.send(pdfDetail);
}
});
}
var saveGoogleDrivePdf= function (url, callback) {
async.waterfall([
function (cb) {
var fileDetails = {
url: url,
name: 'drive/' + url.toString().replace(/^(http|https):\/\//i, ""),
contentType: 'application/pdf'
};
file.uploadPdf(fileDetails);
cb(null, fileDetails);
},function (fileDetails, cb) {
console.log(fileDetails);
//create object for saving the pdf
var pdfDetail = {
pdf_url: fileDetails.url,
pdf_title: 'PDF FILE',
pdf_preview_image: config.defaultPreviewImageUrl,
pdf_file: fileDetails.name,
pdf_website: url.parse(url, false, true).host,
mode: 'pdf'
};
cb(null, pdfDetail );
}
], function (err, pdfDetail ) {
if (err) {
callback(err);
} else {
callback(null, pdfDetail );
}
});
}
上传工作正常,但是当我尝试在S3上查看该文件时,我无法打开它。
任何原因?
谢谢
答案 0 :(得分:0)
要使用JS下载所选文件,请使用此
function downloadFile(file, callback) {
if (file.downloadUrl) {
var accessToken = gapi.auth.getToken().access_token;
var xhr = new XMLHttpRequest();
xhr.open('GET', file.downloadUrl);
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.onload = function() {
callback(xhr.responseText);
};
xhr.onerror = function() {
callback(null);
};
xhr.send();
} else {
callback(null);
}
}