I am struggling to get a file upload script working. I am using a node/express backend with jQuery firing off the ajax request.
Markup:
<input id="audio" type="file" name="audio">
Front end JS:
var formData = new FormData();
formData.append("audio", e.data.audio, 'testname');
$.ajax({
url: 'api/upload',
data: formData,
cache: false,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});
using a custom written module that executes the following and places it into e.data.audio
if(has('fileReader')){
return this.$input[0].files[0] || '';
}
When i select as small audio file to upload and submit, e.data.audio has the following value at the point it is set into the ajax function argument object:
Lack of SO streetcred means I need to put images on imgur https://i.sstatic.net/dAjfb.png
After the request is sent I am using the files property of the request object (req.files) to get access to the file, in order to save it.
exports.upload = function(){
return function(req, res){
console.log(req.files);
if (req.files && req.files.audio){
var file = req.files.audio;
fs.readFile(file.path, function(err, data){
if (err){
res.send(err);
}
var newPath = __dirname + 'public/audio';
fs.writeFile(newPath, data, function(err){
if (err){
res.send(err);
}else{
res.send(true);
}
});
})
}else{
}
}
};
However the issue is that the path always seems to be the clients local path.
Lack of SO streetcred means I need to put images on imgur https://i.sstatic.net/fMvZn.png
Ive done a fair amount of googling and cant seem to find anything along the same lines. I am obviously just missing something basic and need someone to point me in the correct direction.