I've created an AngularJS app which connects to a NodeJS server which uploads a file using ngUpload directive for AngularJS.
The issue is that upon returned data from the server, the client is blocked
Blocked a frame with origin "http://localhost" from accessing a frame with origin "http://localhost:3000". Protocols, domains, and ports must match. ng-upload.min.js:14
Uncaught TypeError: Cannot read property 'body' of undefined
The AngularJS is running on localhost while NodeJS is running on localhost with port 3000 so the calls are cross domain (tried changing to port 80, but didn't work)
This is the form code on the client side:
<form enctype="multipart/form-data" method="POST" action="http://localhost:3000/upload" name="iconForm" id="thumbnail_form" ng-upload>
<div class="submitFormRow fileUpload">
<span class="btn btn-warning uploadBtn" style="z-index: 99;">Upload resume</span>
<input type="file" class="input" name="file" tabindex="-1" size="12" id="file" style="z-index: 999;filter:alpha(opacity=0);">
<input type="hidden" value="{{PositionId}}" id="positionid" name="positionid"/>
<input type="hidden" value="{{user_fullName}}" id="fullname" name="fullname"/>
<input type="hidden" value="{{user_email}}" id="email" name="email"/>
<div class="clear"></div>
</div>
<div class="submitFormRow submitBtn">
<input type="submit" upload-submit="bar(content)" class="btn btn-danger" value="Submit"/>
</div>
<div>{{uploadResponse}}</div>
</form>
This is the code on the NodeJS server:
app.post('/upload', function (req, res) {
setTimeout(
function () {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With");
res.header("Access-Control-Allow-Methods", "GET, PUT, POST");
//res.type('application/json');
var positionId = req.body.positionid ? req.body.positionid : "-1";
var fullName = req.body.fullname ? req.body.fullname : "John Doe";
var email = req.body.email ? req.body.email : "default@default.com";
if (req.files.length == 0 || req.files.file.size == 0)
res.send({"success":false, message: "No File was uploaded", data:""});
else {
var fileType = req.files.file.name.split(".").pop();
if(!checkFileType(fileType)){
res.send({"success":false, message: "Invalid file type", data:""});
}
else {
var file = req.files.file;
fs.readFile(req.files.file.path,function(err,data){
var newPath = __dirname + "/uploads/user_" + new Date().getTime() + "_for_position_" + positionId +"." + fileType;
fs.writeFile(newPath, data, function (err) {
if(err){
res.send({"success":false, message: "file wasn't saved", data:""});
}
else {
var dbSaveCallback = function(err,result){
if(result){
res.send({"success":true, message: "file saved", data:newPath});
}
else {
res.send({"success":false, message: "file wasn't saved", data:""});
}
};
saveApplicantToDatabase(positionId,fullName,email,newPath,dbSaveCallback);
}
});
});
}
}
},
(req.param('delay', 'yes') == 'yes') ? 2000 : -1
);
});
The server returns a jsonp result with data inside it, but inside ngupload.min, it's being blocked and I can't use it.
How do I solve this issue ?