2011-11-13 8 views
27

मैं डेटाबेस में नोडजेएस (और एक्सप्रेस फ्रेमवर्क) पर पोस्ट की गई सहेजी गई छवियों को संभालने का प्रयास कर रहा हूं, और कुछ परेशानी हो रही है। सभी वेब प्रसंस्करण को अनदेखा करते हुए, मुझे लगता है कि मैंने समस्या को संकुचित कर दिया है जिस तरह से बेस 64 एन्कोडिंग नोड में हो रहा है। मेरा मानना ​​है कि नीचे उल्लिखित उदाहरण काम करना चाहिए, लेकिन आउटपुट छवि हमेशा दूषित हो जाती है।नोडजेएस बेस 64 छवि एन्कोडिंग/डिकोडिंग काफी काम नहीं कर रहा है

उदाहरण (1) एक छवि में लोड (2) अगर (image_orig) की एक प्रति सहेजता है तो यह पुष्टि करने के लिए कि नोड फ़ाइल को ठीक से पढ़ सकता है। यह हमेशा काम करता है। (3) मैं छवि और बेस 64 अपनी सामग्री एन्कोड लेता हूं, (4) फिर इसे डीकोड करें। अंतिम आउटपुट छवि (image_decoded) हमेशा दूषित होती है।

सहायता! (Node.js 0.6.0 OSX शेर पर)

console.log("starting"); 
process.chdir(__dirname); 

var fs = require("fs"); 

var image_origial = "image.jpg"; 
fs.readFile(image_origial, function(err, original_data){ 
    fs.writeFile('image_orig.jpg', original_data, function(err) {}); 
    var base64Image = new Buffer(original_data, 'binary').toString('base64'); 
    var decodedImage = new Buffer(base64Image, 'base64').toString('binary'); 
    fs.writeFile('image_decoded.jpg', decodedImage, function(err) {}); 
}); 

उत्तर

95

मुझे लगता है कि आप एन्कोडिंग तर्क थोड़ा के उपयोग गलत समझ रहे हैं। यदि आप एन्कोडिंग 'बाइनरी' निर्दिष्ट करने जा रहे हैं, तो आपको इसे लगातार करने की आवश्यकता है। लेकिन वास्तव में आपको इसकी आवश्यकता नहीं है। आप बफर बनाम द्विआधारी तारों के उपयोग को भ्रमित कर रहे हैं।

// This tells node to load the file into a Buffer 'original_data' because you 
// have not specified an encoding for the returned values. If you provided an 
// encoding, then original_data would be a string with that encoding. 
fs.readFile(image_origial, function(err, original_data){ 

    // This tells node to take that buffer, and write it to the new filename. 
    // Again no encoding is provided, so it will assume a Buffer or utf8 string. 
    fs.writeFile('image_orig.jpg', original_data, function(err) {}); 

    // This tells node to create a new buffer from the old buffer, which means 
    // it will iterate over original_data copying the bytes one at a time. But 
    // they will be identical buffers. It will ignore the 'binary' argument 
    // since the object you are passing isn't a string. 
    // Then it encodes the content of that Buffer to base64, which is fine. 
    var base64Image = new Buffer(original_data, 'binary').toString('base64'); 

    // Here you decode the base64 to a buffer, which is fine, but then you 
    // convert the buffer into a string with encoding 'binary'. This means that 
    // it is a string object whose code points are bytes of the buffer. 
    var decodedImage = new Buffer(base64Image, 'base64').toString('binary'); 

    // Here you try to write that String object to a file. Since the argument you 
    // have given is a string and you have not given an encoding argument for the 
    // write command, then it will assume that 'utf8' is the encoding. It will try to 
    // decode your binary string into a utf8 encoded buffer, and write that buffer. 
    // This will cause it to fail because that encoding conversion is wrong. 
    // Really through, 'binary' is just wrong to use. Buffers are already binary. 
    fs.writeFile('image_decoded.jpg', decodedImage, function(err) {}); 
}); 

यह अगले उदाहरण काम करते हैं लेकिन क्योंकि एनकोडिंग हर समय की जरूरत नहीं है बदल रहा है बहुत अक्षम है होगा, लेकिन मैं तो बस स्पष्ट होना यह दिखाना चाहते हैं। यदि आप वास्तव में एक विशिष्ट एन्कोडिंग चाहते हैं, तो आपको यह सुनिश्चित करना होगा कि आप सुसंगत हैं। उन कार्यों में से प्रत्येक में एक एन्कोडिंग तर्क है।

fs.readFile(image_origial, 'binary', function(err, original_data){ 
    fs.writeFile('image_orig.jpg', original_data, 'binary', function(err) {}); 
    var base64Image = new Buffer(original_data, 'binary').toString('base64'); 
    var decodedImage = new Buffer(base64Image, 'base64').toString('binary'); 
    fs.writeFile('image_decoded.jpg', decodedImage, 'binary', function(err) {}); 
}); 

यह करने का यह सही तरीका है। सब कुछ एक बफर के रूप में रखें, सिवाय इसके कि जब आप इसे बेस 64 बनाते हैं।

fs.readFile(image_origial, function(err, original_data){ 
    fs.writeFile('image_orig.jpg', original_data, function(err) {}); 
    var base64Image = original_data.toString('base64'); 
    var decodedImage = new Buffer(base64Image, 'base64'); 
    fs.writeFile('image_decoded.jpg', decodedImage, function(err) {}); 
}); 
+3

धन्यवाद! यह हल हो गया! – Evan

+0

उस सुपर सूचनात्मक प्रतिक्रिया के लिए धन्यवाद! मैं आपके दिशानिर्देशों का उपयोग करके बेस 64 पर छवियों को एन्कोड करने में सक्षम था। – zzaman

+1

प्रतिक्रिया स्ट्रीम में 'decodedImage' कैसे लिखना है? –

11

थोड़ा बेहतर समाधान संभव सब माइम प्रकार दूर करने के लिए किया जाएगा:

var buff = new Buffer(req.body.imageFile 
    .replace(/^data:image\/(png|gif|jpeg);base64,/,''), 'base64'); 
fs.writeFile('/file/path/', buff, function (err) { 
    console.log('done'); 
}); 

यह @ हर्वे के जवाब देने के लिए है।

+0

क्यों माइम प्रकारों को हटाने की जरूरत है ?? – user1575921

+2

मेटा डेटा केवल ब्राउज़र में उपयोग किया जाता है। यदि आप इसे हटा नहीं देते हैं तो नोड छवि को दूषित करता है। – simo

+0

धन्यवाद, मुझे मिल गया – user1575921

संबंधित मुद्दे