-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Labels
supportQuestions, discussions, and general supportQuestions, discussions, and general support
Description
I POST an image from a canvas element the following way. First I have a form:
<a id="draw"></a>
<h3>Canvas</h3>
<div id="canvas">Click to draw<br/></div>
<form class="form-inline" action="/hello" method="POST" type="multipart/form-data">
<input type="text" name="name" value="" placeholder="name" />
<button class="btn" onclick="return sendCanvasFromHTML(this.form);">Submit canvas as an upload file</button>
</form>which values are posted via javascript:
function sendCanvasFromHTML(form) {
var canvas = document.getElementById('innerCanvas');
var dataURL = canvas.toDataURL();
var file = dataURLtoBlob(dataURL);
var formData = new FormData(form);
formData.append('id', '123456');
formData.append('uploadFile', file, 'canvas.png');
var xhr = new XMLHttpRequest();
xhr.open('POST', form.action, true);
xhr.onload = function(e) { alert(this.responseText) };
xhr.send(formData);
return false;
}This works when using
"hapi": "4.0.x",
"joi": "3.1.x"
but when I switch to the latest version os hapi (see package.json) it fails.
When I use fs.writeFile I get an internal Hapi error:
Received POST name smile.png
f Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 00 96 00 00 00 96 08 06 00 00 00 3c 01 71 e2 00 00 0b d6 49 44 41 54 78 5e ed 9d cb 6a 15 4d 10 80 ...
Debug: hapi, internal, implementation, error
Error: Uncaught error: Second argument needs to be a buffer
at Object.fs.write (fs.js:513:11)
at writeAll (fs.js:908:6)
at fs.js:949:7
at Object.oncomplete (fs.js:107:15)
This is the environment I use (package.json):
{
"name": "hapi-post-example",
"version": "0.0.7",
"description": "HTTP POST example routes for hapi",
"main": "index.js",
"scripts": {
"test": "node test/index.js"
},
"repository": "https://github.com/paullang/hapi-post-example.git",
"author": "Paul Lang",
"license": "BSD",
"engines": {
"node": ">=0.10.5"
},
"dependencies": {
"hapi": "^6.7.1",
"joi": "^4.6.2"
},
"devDependencies": {
"specify": "1.1.x",
"request": "2.21.x"
}
}This is the full program:
var Hapi = require('hapi');
var Joi = require('joi');
var Fs = require('fs');
// Create a server with a host and port
var server = Hapi.createServer('localhost', 8000);
var helloPostHandler = function(request, reply) {
console.log("Received POST name " + request.payload.name);
if (request.payload.uploadFile) {
var f = request.payload.uploadFile;
console.log('f', f);
filename = request.payload.name;
Fs.writeFile(filename, f, function(writeerr) {
if (writeerr) throw writeerr;
reply({
status: 'ok', saved_file: request.payload.name
});
});
}
}
var postHelloConfig = {
handler: helloPostHandler,
validate: {
payload: {
name: Joi.string().min(1).required(),
id: Joi.number().min(100).max(999999999),
uploadFile: Joi.object().optional()
} }
};
// Add the routes
server.route([{
method: 'POST',
path: '/hello',
config: postHelloConfig
},
{
method: 'GET',
path: '/{path*}',
handler: {
directory: { path: './public', listing: false, index: true }
}
}
]);
// Start the server
server.start();Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
supportQuestions, discussions, and general supportQuestions, discussions, and general support