Node.js 存储在sqlite blob中的节点返回映像

Node.js 存储在sqlite blob中的节点返回映像,node.js,sqlite,blob,restify,Node.js,Sqlite,Blob,Restify,我正在使用 我的SQLite数据库有一个places表,该表具有以下结构(id INTEGER、name TEXT、image BLOB) 我试图将响应结果输出为图像(blob包含jpeg图像),但到目前为止还没有成功。 在提出请求后的浏览器中,我将获得图像'http://localhost:8080/place/1 无法显示,因为它包含错误 我正在控制台中正确地输入地名 这是我的密码: var restify=require('restify'); 功能响应(req、res、next){ va

我正在使用 我的SQLite数据库有一个places表,该表具有以下结构
(id INTEGER、name TEXT、image BLOB)
我试图将响应结果输出为图像(blob包含jpeg图像),但到目前为止还没有成功。 在提出请求后的浏览器中,我将获得
图像'http://localhost:8080/place/1 无法显示,因为它包含错误
我正在控制台中正确地输入地名

这是我的密码:

var restify=require('restify');
功能响应(req、res、next){
var fs=要求(“fs”);
var file=“./data.db”;
var exists=fs.existsSync(文件);
var sqlite3=require('sqlite3').verbose();
var db=new sqlite3.Database(文件);
db.get(“从id=?”的位置选择id、名称、图像”,req.params.uid,函数(err,row){
console.log(row.name);
res.header('Content-Type','image/jpeg');
res.end(行图像);
db.close();
});
next();
}
var server=restify.createServer();
get('/place/:uid',respond);
listen(8080,函数(){
console.log(“%s”在%s、server.name、server.url上侦听);
});
更新:当我使用相同的库创建blob时,图像将顺利返回。如果我使用Ruby或Go(我已经测试过的那些)创建blob,那么
node-sqlite3
解码将无法按预期工作


这是怎么回事?非常感谢您的帮助。

使用VARCHAR字段并以字符串形式存储在图像中,然后以字节数组形式检索。 请参阅下面的代码,您可以在其中存储图像并检索。不使用blob。 这对你来说是个简单的方法

 Uri selectedImage = data.getData();
         ImageView imageView=(ImageView)this.findViewById(R.id.imageView1);

        getContentResolver().notifyChange(selectedImage, null);

        ContentResolver cr = getContentResolver();
        Bitmap bitmap;
        try {


            bitmap = android.provider.MediaStore.Images.Media
             .getBitmap(cr, selectedImage);
            imageView.setImageBitmap(bitmap);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] b = baos.toByteArray();
            String encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);

            byte[] bytarray = Base64.decode(encodedImageString, Base64.DEFAULT);
            Bitmap bmimage = BitmapFactory.decodeByteArray(bytarray, 0,
                    bytarray.length);

            myDb.execSQL("INSERT INTO imgtbl VALUES('"+encodedImageString+"');");
            Cursor c= myDb.rawQuery("SELECT * FROM imgtbl", null);

           c.moveToFirst();
           String img=c.getString(0);
           byte[] byarray = Base64.decode(img, Base64.DEFAULT);
           Bitmap bmimg = BitmapFactory.decodeByteArray(byarray, 0,
                   byarray.length);

            ImageView iv=(ImageView)findViewById(R.id.img2);
            ImageView iv1=(ImageView)findViewById(R.id.img3);

            iv.setImageBitmap(bmimg);
            iv1.setImageBitmap(bmimg);


        } catch (Exception e) {
            Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                    .show();

            e.printStackTrace();
        }  

用于VARCHAR字段,并将其作为字符串存储在图像中,然后作为字节数组检索。 请参阅下面的代码,您可以在其中存储图像并检索。不使用blob。 这对你来说是个简单的方法

 Uri selectedImage = data.getData();
         ImageView imageView=(ImageView)this.findViewById(R.id.imageView1);

        getContentResolver().notifyChange(selectedImage, null);

        ContentResolver cr = getContentResolver();
        Bitmap bitmap;
        try {


            bitmap = android.provider.MediaStore.Images.Media
             .getBitmap(cr, selectedImage);
            imageView.setImageBitmap(bitmap);

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            byte[] b = baos.toByteArray();
            String encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);

            byte[] bytarray = Base64.decode(encodedImageString, Base64.DEFAULT);
            Bitmap bmimage = BitmapFactory.decodeByteArray(bytarray, 0,
                    bytarray.length);

            myDb.execSQL("INSERT INTO imgtbl VALUES('"+encodedImageString+"');");
            Cursor c= myDb.rawQuery("SELECT * FROM imgtbl", null);

           c.moveToFirst();
           String img=c.getString(0);
           byte[] byarray = Base64.decode(img, Base64.DEFAULT);
           Bitmap bmimg = BitmapFactory.decodeByteArray(byarray, 0,
                   byarray.length);

            ImageView iv=(ImageView)findViewById(R.id.img2);
            ImageView iv1=(ImageView)findViewById(R.id.img3);

            iv.setImageBitmap(bmimg);
            iv1.setImageBitmap(bmimg);


        } catch (Exception e) {
            Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                    .show();

            e.printStackTrace();
        }  

您正在使用
db关闭数据库。由于查询是异步运行的,因此在查询运行之前关闭
。将
db.close
移到
db.get
回调的末尾。@Colonelthirtyone,还是同一个问题,我根据您的建议更新代码。在查询运行之前,您将使用
db.close
关闭数据库,因为查询是异步运行的。将
db.close
移到
db.get
回调的末尾。@collonelthirtytwo完成,仍然是相同的问题,我根据您的建议更新代码。