This is my server code
app.post('/tde/api/photo/:widgetId/:choosenFileName',function(req,res){
console.log("In file Upload..");
console.log(req.params.widgetId);
console.log(req.params.choosenFileName);
res.writeHead(200, { 'Content-Type': 'application/binary' });
var filedata = '';
req.setEncoding('binary');
req.on('data', function(chunk){
filedata+= chunk;
})
req.on('end', function (chunk) {
var dir = 'uploads/'+req.params.widgetId
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
console.log("directory created..");
}
var fileName = req.params.choosenFileName;
var widgetId = req.params.widgetId;
fs.writeFile('uploads/'+widgetId+'/'+fileName, filedata, 'binary', function(err) {
if (err) {
return console.error(err);
}
})
});
res.end("File is uploaded");
})
This is my client side code. This is my html file which allows user to choose a file and then upload it to the server location
<div class="col-xs-8">
Upload Image
<span class="warn_msg" ng-if="warningModalWidgetSettingsURLMessage1 != ''"><i class="glyphicon glyphicon-remove-circle"></i> {{warningModalWidgetSettingsURLMessage1}} </span>
</div>
This is my js file which calls the api of nodejs server
$scope.widgetForm.imageConfig = {};
var imageLocationUrl = "http://10.100.10.140/opt/TDE/tde_v2.0-19-01-2016Sanmoy/uploads/"
var imageWidgetId = "";
var choosenFileName = "";
$scope.submitFile = function(widgetId){
imageWidgetId = widgetId
console.log($scope.fileImage)//this is the file name which I a
choosenFileName = $scope.fileImage.name
$http({
url: "http://10.100.10.140:9001/tde/api/photo/"+widgetId+"/"+choosenFileName,
method: "POST",
headers: {
'Content-Type': 'application/binary'
},
data: $scope.fileImage,
processData: false,
contentType: false
}).success(function(data, status) {
console.log("File Uploaded..")
}).error(function(data, status) {
console.log("File Upload fail..")
})
}
file is getting uploaded in the server but sometimes it is getting corrupted,can't understand where the problem is.