上载要解析的文件(Javascript)
简而言之,我正在尝试将pdf文档存储到parse中。从本质上讲,它是如何工作的,一个用户从GoogleDrive中选择上传的文件,然后从选择的文档中选择我想要存储到Parse中的文件 我已经能够为从驱动器中选择的pdf文档生成一个URL,并希望使用该URL存储在Parse中 下面是允许用户从google drive中选择pdf文档的代码,以及为该项目生成唯一URL的位置上载要解析的文件(Javascript),javascript,parse-platform,google-drive-api,Javascript,Parse Platform,Google Drive Api,简而言之,我正在尝试将pdf文档存储到parse中。从本质上讲,它是如何工作的,一个用户从GoogleDrive中选择上传的文件,然后从选择的文档中选择我想要存储到Parse中的文件 我已经能够为从驱动器中选择的pdf文档生成一个URL,并希望使用该URL存储在Parse中 下面是允许用户从google drive中选择pdf文档的代码,以及为该项目生成唯一URL的位置 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xh
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>eSnail Scan Upload Part 2</title>
<script type="text/javascript">
// The Browser API key obtained from the Google Developers Console.
var developerKey = 'xxxxxxxxxx';
// The Client ID obtained from the Google Developers Console.
var clientId = 'xxxxxxxxxxxxx';
// Scope to use to access user's photos.
var scope = ['https://www.googleapis.com/auth/photos'];
var pickerApiLoaded = false;
var oauthToken;
// Use the API Loader script to load google.picker and gapi.auth.
function onApiLoad() {
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 picking user Photos.
function createPicker() {
if (pickerApiLoaded && oauthToken) {
var picker = new google.picker.PickerBuilder().
enableFeature(google.picker.Feature.MULTISELECT_ENABLED).
addView(google.picker.ViewId.PDFS).
setOAuthToken(oauthToken).
setDeveloperKey(developerKey).
setCallback(pickerCallback).
build();
picker.setVisible(true);
}
}
// A simple callback implementation.
function pickerCallback(data) {
var url = 'nothing';
if (data[google.picker.Response.ACTION] == google.picker.Action.PICKED) {
var doc = data[google.picker.Response.DOCUMENTS][0];
url = doc[google.picker.Document.URL];
}
var message = 'The following(s) were stored in Parse: ' + url;
document.getElementById('result').innerHTML = message;
}
</script>
</head>
<body>
<div id="result"></div>
<!-- The Google API Loader script. -->
<script type="text/javascript" src="https://apis.google.com/js/api.js?onload=onApiLoad"></script>
</body>
</html>
实际上,我有一个文件的URL,并且希望将该URL存储到Parse中,以便以后能够检索该文件
注:
下面是我发现的一段代码,它允许用户将文件上传到Parse中。问题是我不希望文件来自用户计算机用户上传文件使用文件输入,但从谷歌驱动器提供的URL
<HTML>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
</head>
<body>
<form id="fileupload" name="fileupload" enctype="multipart/form-data" method="post">
<fieldset>
<input type="file" name="fileselect" id="fileselect"></input>
<input id="uploadbutton" type="button" value="Upload to Parse"/>
</fieldset>
</form>
<script type="text/javascript">
$(function() {
var file;
// Set an event listener on the Choose File field.
$('#fileselect').bind("change", function(e) {
var files = e.target.files || e.dataTransfer.files;
// Our file var now holds the selected file
file = files[0];
});
// This function is called when the user clicks on Upload to Parse. It will create the REST API request to upload this image to Parse.
$('#uploadbutton').click(function() {
var serverUrl = 'https://api.parse.com/1/files/' + file.name;
$.ajax({
type: "POST",
beforeSend: function(request) {
request.setRequestHeader("X-Parse-Application-Id", 'pWG7YizRnwxRjplGT9RSLoHtFItDtvmc2EK0YJAe');
request.setRequestHeader("X-Parse-REST-API-Key", '2LsfIAg5Np9u09ScVIT5StEcO0LXMfpzndWOiwHX');
request.setRequestHeader("Content-Type", file.type);
},
url: serverUrl,
data: file,
processData: false,
contentType: false,
success: function(data) {
prompt("File available at: ", data.url);
},
error: function(data) {
var obj = jQuery.parseJSON(data);
prompt(obj.error);
}
});
});
});
</script>
</head>
</body>
</HTML>
如有任何帮助或建议,将不胜感激。提前感谢。我客户的一个项目也遇到了同样的问题。我们用几种不同的方法研究了它,并决定最好的方法是将URL传递到我们自己的服务器并在那里处理上传。在我们的特定实例中,我们将cURL与PHP/codeignator一起使用,抓取文件,然后将其上载到S3,但解析上载也可以轻松完成同样的操作
我们研究了如何在httpRequest中使用CloudCode,但问题是执行超时。Parse只为每个CloudCode函数提供了很短的时间,在我们超时之前,文件永远不会完成下载。从这个意义上讲,如果同时发送10个PDF文档URL进行解析,我会有机会遇到超时吗?我注意到Parse最近为file Parse.file添加了一个新类,您有过这样的经验吗-?我不确定我是否理解您关于同时发送10个PDF文档的问题。您可以同时上载10个Parse.Objects,因此同时保存10个URL不会成为问题。至于Parse.File,它已经存在了相当长的一段时间,但是您需要向它发送数据的Base64字符串表示形式…这意味着您需要首先从URL下载PDF并转换它。您确实需要在服务器端执行此操作。