2014-11-04 43 views
0

我試圖用nano來插入一個帶有多個圖像附件的文檔。下面的測試用例運行無故障:couchdb /納米多部分插入

var dbName = 'testdb'; 
var encoding = 'base64'; 

var fs = require('fs'); 
var nano = require('nano')('http://localhost:5984'); 

nano.db.create(dbName); 
var db = nano.use(dbName); 

var attach1 = { 
    name: 'image_1', 
    data: fs.readFileSync('test_image.jpg').toString(encoding), 
    content_type: 'image/jpeg' 
}; 

var attach2 = { 
    name: 'image_2', 
    data: fs.readFileSync('test_image_2.jpg').toString(encoding), 
    content_type: 'image/jpeg' 
}; 

var doc = { 
    _id: 'test_id', 
    html: fs.readFileSync('test_html.html').toString() 
}; 

db.multipart.insert(doc, [attach1, attach2], doc._id, function(err){ 
    if (err) 
    console.log('failed: ' + err); 
    else 
    console.log('succeeded'); 
}); 

然而,當我嘗試使用蒲團查看圖像,他們似乎打破了,如果我下載的圖片,並嘗試打開它們,我被告知「錯誤解讀JPEG圖像文件(不是JPEG文件:以0x2f 0x39開頭)「。

這些文件具有大致正確的大小(在服務器上稍大一些,但不是太多),所以我猜這裏有一些格式錯誤。這就是說,我已經嘗試了二進制和utf8作爲編碼值,我不知道還有什麼其他的辦法。

在此先感謝。

回答

2

的數據應該是一個緩衝區,而不是base64編碼字符串,你可以使用的fs.readFileSync結果:

var attach1 = { 
    name: 'image_1', 
    data: fs.readFileSync('test_image.jpg').toString(encoding), 
    content_type: 'image/pjpeg' 
}; 

需要注意的是納米高達5.11.2使用多字符串時,曾與unicode字符的錯誤插入,見https://github.com/dscape/nano/pull/225

+0

謝謝Johannes。我也嘗試過 - 但是你標記的修復會引發它自己的錯誤,因爲Buffer.byteLength(data)顯然需要一個字符串。我意識到這是最近的一次變化 - 這是否意味着納米目前可能存在問題,還是我只是一個白癡? 注:content_length中的固定類型 - 不改變結果alas。 – DomJack 2014-11-06 22:41:54

+0

我想你碰到了https://github.com/dscape/nano/issues/231,但它還沒有登陸npm。 – 2014-11-06 22:50:22

+0

請升級到5.12.2 – 2014-11-06 23:08:37