I am uploading a video file through node.acs. Videos with 1 or 2 mb gets uploaded easily but above that itgets upload but before it uploaded, is giving error that 'Error Occurred while uploading file'.
Here is my code.
<form id=imageform_<%=video.id%> name=imageform_<%=video.id%> method="POST" enctype="multipart/form-data" action="/api/uploadphoto">
<img id=image_<%=video.id%> src="images/upload.png" width="80" height="100" border="1" />
<input type="hidden" name="recordid" value='<%=video.id%>' >
<input type="hidden" name="classname" value='Video' >
<div id="progress" hidden="true" class="row-fluid">
<img id=image_<%=video.id%> src="images/uploading.gif" width="150" height="5" border="1" />
</div>
<div id="file_input" class="row-fluid">
<input type="file" name="photo_file" id=photo_file_<%=video.id%> onchange="readImage(this,'<%=video.id%>')"/>
</div>
<button id="btnUpload" class="btn btn-primary" disabled onClick="formSubmitHandler1(this,'<%=video.id%>')">Upload</button>
function formSubmitHandler1(btn, recid) { // Get form element from document var formId = "#imageform_" + recid; var formObj = $(formId);
formObj.submit(function(e)
{
e.preventDefault(); //Prevent Default action.
});
// Hide the upload button and input
btn.style.display = 'none';
formObj.find("#file_input").hide();
// Show progress
formObj.find("#progress").show();
var formURL = formObj.attr("action");
var formData = new FormData(formObj[0]);
$.ajax({
url: formURL,
type: 'POST',
data: formData,
mimeType:"multipart/form-data",
contentType: false,
cache: false,
processData:false,
success: function(data, textStatus, jqXHR)
{
alert("Image uploaded successfully !");
// Parse data sent by server into JSON
var datajson = JSON.parse(data);
// Set new image for this product
var videoImgId = "#video_image_" + recid;
$(videoImgId).attr('src',datajson.Video[0].photo_url);
// Hide progress
formObj.find("#progress").hide();
// show upload button and file input
btn.style.display = 'block';
formObj.find("#file_input").show();
formObj.find("#file_input")[0].children[0].value = '';
// Disable upload button & reset image
formObj.find("#btnUpload").attr('disabled','disabled');
var imgId = '#image_' + recid;
$(imgId).attr('src',"images/upload.png").width('100').height('100');
},
error: function(jqXHR, textStatus, errorThrown)
{
alert(textStatus);
console.log(errorThrown);
}
});
}
Then it calls a function on js as follows
function uploadfile(req,res) { // Get the class name for custom object and it's ID to update it var objid = req.body.recordid; var objname = req.body.classname;
// console.log(require('path').dirname('config.json'));
var data = {
session_id:req.session.session_id,
name: 'A/V File',
file: req.files.file
};
ACS.Files.create(data, function(e) {
if(e.success && e.success === true){
ACS.Objects.update({
session_id:req.session.session_id,
classname:objname,
id:objid,
fields: {
file_id:e.files[0].id,
Status:'Uploaded',
}
},function(data) {
if(data.success) {
// console.log('Updated successfully:' + JSON.stringify(data));
res.send(data);
//res.redirect('/video');
}else {
console.log('Error:\n' +
((data.error && data.message) || JSON.stringify(data)));
}
});
}else{
logger.debug('Error: ' + JSON.stringify(e));
req.session.flash = {msg:e.message, r:0};
res.redirect('/');
}
});
}