gpt4 book ai didi

javascript - 解析 JavaScript 上传或更新文件匹配表中行的 objectId

转载 作者:行者123 更新时间:2023-11-28 08:02:43 25 4
gpt4 key购买 nike

我也是 Parse.com 的新手。我已经通过 Javascript 完成了上传文件,但我想通过查找“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>

最佳答案

我解决了。只需将第二个“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);
}
});
});
});

关于javascript - 解析 JavaScript 上传或更新文件匹配表中行的 objectId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25201834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com