2012-09-19 12 views
6

मैं express3.x पर हूँ ट्रिगर औरNodeJS दुर्जेय किसी भी घटनाओं

app.post "/up",(req,res)-> 
formidable = require('formidable') 
form = new formidable.IncomingForm() 
form.uploadDir = __dirname + "/uploads" 
form.encoding = 'binary' 


form.addListener 'file',(name,file) -> 
    #write file 
    console.log('file uploaded') 
    return 
console.log('$$$$$$$$$$$') 
form.addListener 'end', -> 
    console.log('end') 
    res.end() 
    return 
console.log('@@@@@$$$$') 
form.parse req,(err,fields,files)-> 
    console.log('parse') 
    console.log(err) if(err) 
    return 
console.log('######') 
return 

दुर्जेय का उपयोग करने और अपलोड फ़ॉर्म

block content 
:coffeescript 
    $ -> 
     formData = new FormData() 
     xhr = new XMLHttpRequest() 
     onProgress = (e)-> 
      per_complete = (e.loaded/e.total) * 100 if e.lengthComputable 

     onReady = (e)-> 
      alert("Ready") 

     onError = (err)-> 
      alert("Error Loading "+err) 

     $('#upload').click (e)-> 
      formData.append('img',document.getElementById('img').files[0]) 
      xhr.open('post','/up',true) 
      xhr.addEventListener('error',onError,false) 
      xhr.addEventListener('progress',onProgress,false) 
      xhr.send(formData) 
      xhr.addEventListener('readystatechange',onReady,false) 

h1 hello world 
form 
    |select Image: 
    input(type='file',name='img', id='img') 
    input(type='button',value='button', id='upload') 

घटनाओं में से कोई भी शुरू हो रहा हो रहे हैं ... सुनिश्चित नहीं हैं कि क्या कर रहा हूँ है नहीं मैं अपना लापता हूं ..

उत्तर

1

क्या कोई विशिष्ट कारण है कि आप भयानक उपयोग कर रहे हैं और bodyParser में निर्मित नहीं हैं? यह multipart मिडलवेयर का उपयोग करता है और यह भयानक पर आधारित है। इसलिए formidable के अधिकांश विकल्पों को भी bodyParser पर लागू किया जा सकता है। उदाहरण के लिए:

app.js

app.configure(function(){ 
    app.set('port', process.env.PORT || 3000); 
    app.set('views', __dirname + '/views'); 
    app.set('view engine', 'jade'); 
    app.use(express.favicon()); 
    app.use(express.logger('dev')); 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(app.router); 
    app.use(express.static(path.join(__dirname, 'public'))); 
}); 

app.post("/", function(req, res) { 
    console.log(req.files.img) 
    res.end() 
}) 

index.jade

form 
    input(type="file", name="img", id="img") 
    input(type="button", value="button", id="upload") 
script 
    var formdata = new FormData() 
    var xhr = new XMLHttpRequest() 

    $("#upload").on("click", function(e) {    
    xhr.upload.onprogress = function(evt) { 
     console.log("While sending and loading data.") 
     if (evt.lengthComputable) { 
     console.log (evt.loaded/evt.total * 100); 
     } 
    } 

    xhr.onloadend = function(evt) { 
     console.log("When the request has completed (either in success or failure).") 
    } 

    xhr.onload = function(evt) { 
     console.log("When the request has successfully completed.") 
    } 

    var image = document.getElementById('img').files[0] 
    formdata.append("img", image) 
    xhr.open("POST", "/", true) 
    xhr.send(formdata) 
    }) 

W3C Specifications पर एक नज़र डालें:

app.use(connect.multipart({ uploadDir: path })); 

आपके प्रश्न का उत्तर करने के लिए निम्न कोड की कोशिश अधिक घटनाओं के लिए।

2

एक्सप्रेस आंतरिक रूप से दुर्जेय का उपयोग कर, है तो अपने दुर्जेय कोई ईवेंट नहीं मिलता है, अगर आप अपने दम पर दुर्जेय उपयोग करने के लिए आप bodyParser से multipart/form-data दूर करने के लिए

मैं एक्सप्रेस 2.x पर अभी भी कर रहा हूँ है चाहते हैं लेकिन वह भी 3.x पर चाल करना चाहिए, अपने app.configure समारोह में

app.configure(function(){ 
    delete express.bodyParser.parse['multipart/form-data'] 
}) 

अब आप अपने दम पर दुर्जेय उपयोग कर सकते हैं की कोशिश करो।

1

मेरे पास एक ही समस्या थी जिसमें फ़ाइल को पूरी तरह से अपलोड किया गया था इससे पहले कि मेरा नियंत्रक इसे प्राप्त करने के लिए तैयार हो गया। घटनाओं को निकाल दिया गया था लेकिन मैं अभी तक नहीं सुन रहा था। इस अधिनियम का मुकाबला करने के लिए मैंने फ़ाइल अपलोड को कैप्चर करने के लिए एक मिडलवेयर लिखा और बाद में पहुंचने के लिए उन्हें नियंत्रक के लिए चारों ओर रखा।

https://gist.github.com/3813103

आप

var fileHandler = require('./middleware/fileHandler'); 
app.use(fileHandler()); 
3

साथ इसे लागू करता है, तो आप का उपयोग app.use(express.bodyParser()) के बराबर है सकते हैं:

app.use(express.json()); 
app.use(express.urlencoded()); 
app.use(express.multipart()); 

आप app.use(express.multipart()); निकाल सकते हैं तो आप दुर्जेय वस्तु का उपयोग कर सकते हैं।