我也是Parse.com上的新人。我已经通过Javascript完成了上传文件,但是我想通过find" objectId"来对照课程中的行进行文件上传。但我无法找到任何解决方案:(
以下是我上传文件的代码......
<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.15.min.js"></script>
<script type="text/javascript">
Parse.initialize("2Rsgjz6VQiozYpqQClfmBHQASTTtrdQpUoe2", "fIccKcC9sMH48CKkGYIk62wsA8tTgPpByip");
$(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", '2Rsgjz6VQiozYpxWBfmBHQASTTtrdQpUoe2');
request.setRequestHeader("X-Parse-REST-API-Key", 'UPxPXDIkkC6lc52TYpQZ4bX5DjrWH9Wy7');
request.setRequestHeader("Content-Type", file.type);
},
url: serverUrl,
data: file,
processData: false,
contentType: false,
dataType: 'json',
success: function(data) {
var file_input = document.getElementById("url").innerHTML = '<a href="' + data.url + '">'+ data.url + '</a>';
var classUrl = "https://api.parse.com/1/classes/sampleClass"
if(data) {
var fileName = "" + data.name;
$.ajax({
type: "POST",
beforeSend: function(request) {
request.setRequestHeader("X-Parse-Application-Id", '2Rsgjz6VQiozYpqQClcxWBfmBHQASTTtrdQpUoe2');
request.setRequestHeader("X-Parse-REST-API-Key", 'UPxPXDIkkCLY2M2b56lc52TYpQZ4bX5DjrWH9Wy7');
request.setRequestHeader("Content-Type", 'application/json');
},
url: classUrl,
data: '{"upload" : {"name" : '+"\""+fileName+"\""+', "__type" : "File"}}',
processData: false,
success: function(data) {
console.log("Image successfully uploaded.");
},
error: function(error) {
console.log("Error: " + error.message);
}
});
} else {
//Data is null
console.log("Data IS NULL");
}
},
error: function(data) {
var obj = jQuery.parseJSON(data);
alert(obj.error);
}
});
});
});
</script>
</head>
<body>
<div id="url">
</div>
<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>
</body>
</html>
答案 0 :(得分:0)
我解决了。只需将第二个“ajax”“POST”替换为“PUT”,并在url中设置要更新的对象的“objectId”以更新现有文件。 我的代码如下......
$(document).ready(function() {
//Init Parse SDK
Parse.initialize("yourappkey", "yourjskey");
//File Upload
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", 'yourappid');
request.setRequestHeader("X-Parse-REST-API-Key", 'yourrestapikey');
request.setRequestHeader("Content-Type", file.type);
},
url: serverUrl,
data: file,
processData: false,
contentType: false,
dataType: 'json',
success: function(data) {
//Change variable to reflect your class to upload to
var classUrl = "https://api.parse.com/1/classes/ClassName/objectId"
if(data) {
var fileName = "" + data.name;
$.ajax({
type: "PUT",
beforeSend: function(request) {
request.setRequestHeader("X-Parse-Application-Id", 'yourappid');
request.setRequestHeader("X-Parse-REST-API-Key", 'yourrestapikey');
request.setRequestHeader("Content-Type", 'application/json');
},
url: classUrl,
data: '{"upload" : {"name" : '+"\""+fileName+"\""+', "__type" : "File"}}',
processData: false,
success: function(data) {
console.log("Image successfully uploaded.");
},
error: function(error) {
console.log("Error: " + error.message);
}
});
} else {
//Data is null
console.log("Data IS NULL");
}
},
error: function(data) {
var obj = jQuery.parseJSON(data);
alert(obj.error);
}
});
});
});