如何将图像作为多部分/表单数据从android上传到Nodejs(gridfs是我存储图像的方式)
我计划用截击库来解决这个问题。有一些如何将图像作为多部分/表单数据从android上传到Nodejs(gridfs是我存储图像的方式),android,node.js,mongodb,express,gridfs,Android,Node.js,Mongodb,Express,Gridfs,我计划用截击库来解决这个问题。有一些org.apache.httplibrary(我认为这是不推荐的)。但我想在最新的buld上构建我的应用程序 我正在使用gridfs保存图像, ie 在webside(浏览器服务器)上,我首先将映像保存到服务器上的临时目录,然后使用gridfs将其写入mongodb。(我不知道这是不是正确的方法!) 如何从android上传图像截击更好吗?或者哪种方法最好? 你能提供一个很好的指南吗?或者请分享一些代码。我已经成功地使用了改型2-| 您可以使用此答案中的代码:
org.apache.http
library(我认为这是不推荐的)。但我想在最新的buld上构建我的应用程序
我正在使用gridfs保存图像,
ie
在webside(浏览器服务器)上,我首先将映像保存到服务器上的临时目录,然后使用gridfs将其写入mongodb。(我不知道这是不是正确的方法!)
如何从android上传图像<代码>截击更好吗?或者哪种方法最好?
你能提供一个很好的指南吗?或者请分享一些代码。我已经成功地使用了改型2-| 您可以使用此答案中的代码: 当您上传图像时,您会发出如下请求: 其中“image”是我相信的文件名。我希望这有帮助
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");
}
@Multipart
@POST("uploadAttachment")
Call<MyResponse> uploadAttachment(@Part MultipartBody.Part filePart);
// You can add other parameters too
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);
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
})