我已经在Node.js
中编写了图片上传API,在Postman中可以正常工作,但是在Android中我没有任何办法做到这一点。我已经尝试过this library
但仍然无法解决。我的上传图片API如下
var upload = multer({ dest: '/tmp/'});
app.post('/file_upload', upload.single('file'), function(req, res) {
var file = 'images' + '/' + req.file.filename+'.jpg';
fs.rename(req.file.path, file, function(err) {
if (err) {
console.log(err);
res.send(500);
} else {
res.json({
message: 'File uploaded successfully',
filename: req.file.filename
});
}
});
});
离子库的实现如下
Ion.with(this)
.load("http://104.131.162.126:3001/file_upload")
.setMultipartParameter("name", "source")
.setMultipartFile("image", "image/jpeg", f)
.asJsonObject()
.setCallback(new FutureCallback<JsonObject> () {
@Override
public void onCompleted(Exception e, JsonObject result) {
//do stuff with result
dialog.dismiss ();
Toast.makeText (NewActivity.this,"Image uploaded",Toast.LENGTH_LONG).show ();
}
});