图片上传 Android + Sails.js [英] Image uploading Android + Sails.js

查看:38
本文介绍了图片上传 Android + Sails.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有哪些可能的方式/库可以将图像从 android 应用上传到sails.js Node.js 服务器?

What are the possible ways/libraries available to upload images from android app to sails.js Node.js server?

我遇到的一种实现方法是从应用程序发送 Base64 编码的位图图像字符串并将其保存到数据库,但这似乎是处理多个大尺寸图像的低效方法,因为 Base64 编码的字符串是 33%大于原始尺寸.

One way that I came across to achieve this is sending Base64 encoded Bitmap image string from the app and saving it to the database, but this seems to be an inefficient way to handle multiple big size images as Base64 encoded string is 33% larger than raw size.

另一种方法是将图像作为多部分表单数据发送,但我找不到很好的例子.请提供示例来演示如何从应用程序发送图像并在服务器端处理它(node.js/sails.js)

Another way is to send images as multipart form data, but I couldn't found good examples on this. Please, provide examples which demonstrate how to send images from app and handle it at serverside (node.js/sails.js)

是否有其他推荐的库可用于处理 android 中的图像上传?

Are there any other recommended libraries available to handle image uploading in android?

推荐答案

Sails.js Backend文件上传代码和文件会上传到assets/images文件夹

Sails.js Backend file upload code and file will be uploaded in assets/images folder

upload: function (req, res) {
        if (req.method === 'GET')
            return res.json({
                'status': 'GET not allowed'
            });
        //  Call to /upload via GET is error

        var data = req.file('uploadFile');

        data.upload({
            dirname: '../../assets/images'
        }, function onUploadComplete(err, files) {
            // Earlier it was ./assets/images .. Changed to ../../assets/images
            //  Files will be uploaded to ./assets/images
            // Access it via localhost:1337/images/file-name
            console.log(files);
            if (err) {
                console.log(err);
                return res.json(500, err);
            } else if (files.length === 0) {
                // proceed without files
                res.notFound({
                    status: 'Unsucess',
                    response: 'File not Uploaded'
                });
            } else {
                //  handle uploaded file
                res.json({
                    status: 200,
                    file: files
                });
            }
        });
    }

安卓代码:-

    RequestBody requestBody = new MultipartBody.Builder()  
            .setType(MultipartBody.FORM)
            .addFormDataPart("fileUploadType", "1")
            .addFormDataPart("miniType", contentType)
            .addFormDataPart("ext", file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf(".")))
            .addFormDataPart("fileTypeName", "img")
            .addFormDataPart("clientFilePath", selectedImageUri.getPath())
            .addFormDataPart("filedata", filename + ".png", fileBody)
            .build();

    Request request = new Request.Builder()
                .url(API_URL)
                .post(requestBody)
                .build();


OkHttpClient okHttpClient = new OkHttpClient();  
okHttpClient.newCall(request).enqueue(new Callback() {  
    @Override
    public void onFailure(Call call, final IOException e) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                et_response.setText(e.getMessage());
                Toast.makeText(MainActivity.this, "nah", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public void onResponse(Call call, final Response response) throws IOException {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    et_response.setText(response.body().string());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Toast.makeText(MainActivity.this, "response: " + response, Toast.LENGTH_LONG).show();
            }
        });
    }
});

安卓代码可以参考

http://blog.aimanbaharum.com/2016/03/26/android-image-multi-part-upload/

这篇关于图片上传 Android + Sails.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆