关于这个问题有很多帖子,但没有一个帮助过我。错误代码无济于事。
这是我的服务器端代码 - 我正在使用Node + Express并将文件上传到服务器端的Parse:
exports.changeProfilePicture = function(req, res) {
console.log("exports.changeProfilePicture -- start");
var sessionToken = req.session.sessionToken;
Parse.User.become(sessionToken).then(function (user) {
// var fstream;
req.pipe(req.busboy);
req.busboy.on('file', function (fieldname, file, filename) {
var parseFile = new Parse.File(filename, {base64: file.toString('base64', 0, file.length)}); // Convert from buffer object to array
console.log("parseFile");
console.log(parseFile);
parseFile.save().then(function(file) {
console.log("file.url = " + file.url);
console.log(file);
user.set('profilePictureMedium', file);
user.save().then(function() {
console.log("exports.changeProfilePicture -- 200");
// res.send({status:200});
res.status(200).send();
}, function(error) {
console.log("Error: " + error.code + " " + error.message);
res.status(503).send({ errorCode: error.code, errorMessage: error.message});
});
},
function(error) {
// The file either could not be read, or could not be saved to Parse.
console.log("parseFile.save() -- Error: " + error.code + " " + error.message);
res.status(503).send({ errorCode: error.code, errorMessage: error.message});
});
});
}, function (error) {
// The token could not be validated.
console.log("exports.logout -- become -- error = " + error);
res.status(503).send();
});
这是客户端AJAX:
var fileUploadControl = $(".image-preview-input input:file")[0];
if (fileUploadControl.files.length > 0) {
var file = fileUploadControl.files[0];
var formdata = new FormData();
formdata.append("file", file);
formdata.append("coords", JSON.stringify(coordInfo));
$.ajax({
url: '/changeProfilePicture',
type: 'post',
xhr: function() { // custom xhr
myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // check if upload property exists
// for handling the progress of the upload
myXhr.upload.addEventListener('progress',progressHandlingFunction, false);
}
return myXhr;
},
success: function(data) {
console.log("post success = " + data.errorCode + data.errorMessage);
if (data.status === 200) {
window.location = '/home/language';
}
// If an error, show the prompt
if (data.errorCode === 101) {
$('#login-alert').show().text("Incorrect Login.");
}
// res.redirect('/home/language?language=' + defaultLanguage);
},
error: function(data){
alert("xhr error");
console.log("post error = " + data.errorCode + data.errorMessage);
console.log("error");
console.log(data);
},
// Form data
data: formdata,
//Options to tell JQuery not to process data or worry about content-type
cache: false,
contentType: false,
processData: false
});
}
客户端我在帖子上收到错误结果:
POST https://20a09825.ngrok.com/changeProfilePicture 502 (Bad Gateway)
服务器端我得到以下内容。 正如您所看到的,Parse调用都不会返回错误。我可以保存Parse.File对象,将其设置在用户对象上然后保存它没有错误。然而,链接被破坏,尽管服务器日志显示我正在返回200代码,我收到了客户端错误:
10:53:32 web.1 | exports.changeProfilePicture -- start
10:53:33 web.1 | parseFile
10:53:33 web.1 | { _name: 'monkey_mad.jpg',
10:53:33 web.1 | _source:
10:53:33 web.1 | { _resolved: true,
10:53:33 web.1 | _rejected: false,
10:53:33 web.1 | _resolvedCallbacks: [],
10:53:33 web.1 | _rejectedCallbacks: [],
10:53:33 web.1 | _result: { '0': '[object Object]', '1': 'image/jpeg' } } }
10:53:33 web.1 | file.url = function () {
10:53:33 web.1 | return this._url;
10:53:33 web.1 | }
10:53:33 web.1 | { _name: 'tfss-a7e96b68-37c4-4821-a54c-50dc4e848123-monkey_mad.jpg',
10:53:33 web.1 | _source:
10:53:33 web.1 | { _resolved: true,
10:53:33 web.1 | _rejected: false,
10:53:33 web.1 | _resolvedCallbacks: [],
10:53:33 web.1 | _rejectedCallbacks: [],
10:53:33 web.1 | _result: { '0': '[object Object]', '1': 'image/jpeg' } },
10:53:33 web.1 | _previousSave:
10:53:33 web.1 | { _resolved: true,
10:53:33 web.1 | _rejected: false,
10:53:33 web.1 | _resolvedCallbacks: [ [Function] ],
10:53:33 web.1 | _rejectedCallbacks: [ [Function] ],
10:53:33 web.1 | _result: { '0': [Circular] } },
10:53:33 web.1 | _url: 'http://files.parsetfss.com/b4641fde-4f17-4743-bbe3-447c6c9c50c4/tfss-a7e96b68-37c4-4821-a54c-50dc4e848123-monkey_mad.jpg' }
10:53:34 web.1 | exports.changeProfilePicture -- 200
10:53:34 web.1 | POST /changeProfilePicture 200 1661ms