0

I planned to use volley library to solve this. there are some org.apache.http library (which is deprecated i think). but i want to build my app on latest buld.

i am saving images using gridfs, ie;

var form = new formidable.IncomingForm();
    form.keepExtensions = true;     //keep file extension

    form.parse(req, function (err, fields, files) {

var writestream = gfs.createWriteStream({
                        filename: fields.temp_name,
                        mode: 'w',
                        content_type: 'image/png'
                    });
                    fs.createReadStream(files.template.path).pipe(writestream);


                    writestream.on('close', function (file) {
                        // do something with `file`
                        console.log(file.filename + ' Written To DB');
                        if(err){
                            res.send("error")
                        }else{
                            // do whatever you want
                            console.log("Upload Session name: "+"upload_complete"+req.session.username);
                            io.emit("upload_complete"+req.session.username, "File Uploaded Successfully");
                            //io.emit("upload_complete", "File Uploaded Successfully");
                        }
                    });

            }else{
                io.emit("upload_error"+req.session.username, "Duplicate Name");
            }

on webside (browser-server) i first save image to temp directory on server then i write into mongodb using gridfs. (i dont know if this is the correct way!)

how to upload images from android? volley is better ? or which is the best way to do this? can you provide a good guide to do this? or please share some code..

1 Answers1

1

I've had success working with Retrofit2 - https://square.github.io/retrofit/ | https://github.com/square/retrofit

You can use the code from this answer: https://stackoverflow.com/a/38891018/4455570

When you're uploading an image you make a request as so:

@Multipart
@POST("uploadAttachment")
Call<MyResponse> uploadAttachment(@Part MultipartBody.Part filePart); 
                               // You can add other parameters too

And then you attach the image to the request as so:

File file = // initialize file here

MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", 
file.getName(), RequestBody.create(MediaType.parse("image/*"), file));

Call<MyResponse> call = api.uploadAttachment(filePart);

In Node.js / Express.js you can accept the incoming image using multer: https://github.com/expressjs/multer

In your route you can pre-process the image using the example below:

app.post('/uploadAttachment', upload.single('image'), function (req, res, next) {
// req.file is the `avatar` file
// req.body will hold the text fields, if there were any
})

where "image" is the name of the file I believe. I hope this helps!

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Gabriel Wamunyu
  • 734
  • 9
  • 25