Image Uploading Android + Sails.js
What are the possible ways/libraries available to upload images from android app to sails.js Node.js server? One way that I came across to achieve this is sending Base64 encoded Bi
Solution 1:
I use Multer to handle file uploads via multipart form data.
Out of the box it can do memory and disk storage. By using plugin modules you can use Multer to send files direct to S3 or other storage providers.
Solution 2:
For backend level, use this piece of code in your SailsJS application:
uploadPhoto: function (req, res) {
req.file('photo').upload({
adapter: require('skipper-s3'),
key: S3_KEY,
secret: S3_SECRET,
bucket: IMAGE_BUCKET_NAME,
dirname: DIRECTORY_NAME,
region: S3_REGION
}, function (err, uploaded) {
if(err) {
//Image not uploaded//Returned with error
} elseif(uploaded.length == 0) {
//Image not uploaded
} else {
//Image uploaded//Returned Image Pathvar imagePath = uploaded[0].extra.Location;
}
});
},
And you need to send the file using multipart request. I am doing it with retrofit library. Here is the android code:
1.CreateMultipartRequestBodyObjectRequestBody file =
RequestBody.create(MediaType.parse("multipart/form-data"), photo); //photo is of type "File"2.HandlingRequestBodyinInterface@Multipart@POST("api/uploadphoto")
Call<ResponseBody> uploadPhoto(@Part("photo\"; filename=\"pp\"") RequestBody file);
3.Then initiating the call to server
call.enqueue(newCallback<ResponseBody>() {
@OverridepublicvoidonResponse(Response<ResponseBody> response, Retrofit retrofit) {
Log.e("RESPONSE", response.body());
}
}
@OverridepublicvoidonFailure(Throwable t) {
Log.e("UploadPhoto", "failed");
}
});
That will upload the image to S3-Bucket.
Solution 3:
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 errorvar data = req.file('uploadFile');
data.upload({
dirname: '../../assets/images'
}, functiononUploadComplete(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-nameconsole.log(files);
if (err) {
console.log(err);
return res.json(500, err);
} elseif (files.length === 0) {
// proceed without files
res.notFound({
status: 'Unsucess',
response: 'File not Uploaded'
});
} else {
// handle uploaded file
res.json({
status: 200,
file: files
});
}
});
}
Android Code :-
RequestBodyrequestBody=newMultipartBody.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();
Requestrequest=newRequest.Builder()
.url(API_URL)
.post(requestBody)
.build();
OkHttpClientokHttpClient=newOkHttpClient();
okHttpClient.newCall(request).enqueue(newCallback() {
@OverridepublicvoidonFailure(Call call, final IOException e) {
runOnUiThread(newRunnable() {
@Overridepublicvoidrun() {
et_response.setText(e.getMessage());
Toast.makeText(MainActivity.this, "nah", Toast.LENGTH_SHORT).show();
}
});
}
@OverridepublicvoidonResponse(Call call, final Response response)throws IOException {
runOnUiThread(newRunnable() {
@Overridepublicvoidrun() {
try {
et_response.setText(response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
Toast.makeText(MainActivity.this, "response: " + response, Toast.LENGTH_LONG).show();
}
});
}
});
For Android Code You can Refer to
http://blog.aimanbaharum.com/2016/03/26/android-image-multi-part-upload/
Post a Comment for "Image Uploading Android + Sails.js"