jeudi 19 février 2015

File upload using NodeJs in Rails app

I have a rails application for uploading file things that is working fine but to improve the performance i decided to go with Node Js


If i clicked on submit it is going throw normal way ,,it should go bu node js



View File

<%= form_tag({:controller => "question_bank", :action => "upload_question_bank"}, {:class => "schedule_form", :id => "schedule_form", :enctype => "multipart/form-data"}) do %>
<div class="group_fixed">
<%= label_tag :Syllabus %>
<span>:</span>
<%= file_field_tag "qbank_import", {:class => 'textBoxWidth'} %>
</div>

<div class="actions">
<%= submit_tag "Upload Syllabus", {:class=>'submit_button', :id => "submit_qbank"}%>
</div>
</div>

<%end%>


Routes.rb



resources :question_bank do
post "upload_question_bank", :on => :collection
end


Controller.rb



def upload_question_bank
file_format = `file -ib "#{params[:qbank_import].path}"`.gsub(/\n/,"").split("\;")[0]
if file_format == "application/pdf"
asset = DigitalAsset.save_asset(@site, params[:qbank_import].dup,request.remote_ip)
asset.update_attributes(params[:digital_asset])
asset.asset_type="question-bank"
asset.uploaded_for = params[:uploaded_for]
asset.academic_session_id = @academic_session.id
asset.remote_ip = request.remote_ip
asset.save
end
redirect_to "/syllabus"
end


Running Node Js ## node Server.js That is working fine


Link that i`m following file-uploads-using-node-js


Server.js



var express = require("express");
var multer = require('multer');
var app = express();
var done = false;

/*Configure the multer.*/

app.use(multer({ dest: './uploads',
rename: function (fieldname, filename) {
return filename+Date.now();
},
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...')
},
onFileUploadComplete: function (file) {
console.log(file.fieldname + ' uploaded to ' + file.path)
done=true;
}
}));

/*Handling routes.*/

//app.get('/',function(req,res){
// res.sendfile("index.html");
//});

app.post('/question_bank/upload_question_bank',function(req,res){
if(done==true){
console.log(req.files);
res.end("File uploaded.");
}
});

/*Run the server.*/
app.listen(3000,function(){
console.log("Working on port 3000");
});


package.json



{
"name": "file_upload",
"version": "0.0.1",
"dependencies": {
"express": "^4.10.8",
"multer": "~0.1.6"
}
}


Any help would be greatly appreciate. Do comment if any one need any extra information.


Aucun commentaire:

Enregistrer un commentaire