我是android的新手,我想将多个文件上传到我的Node.js服务器,我正在使用volley生成请求。
我需要在数组中存储多个图像,然后需要将该数组传递给服务器。
我可以将一个文件上传到服务器,但是我不能上传多个文件,任何人都可以帮助我如何实现这一目标。
谢谢
我关注Retrofit 2 — How to Upload Multiple Files to Server
但是我想通过凌空抽射。
我的代码是
private void showChooser() {
ArrayList<Integer> moviesList = new ArrayList<>();
moviesList.add(R.drawable.cart);
moviesList.add(R.drawable.carts);
moviesList.add(R.drawable.userimage);
moviesList.add(R.drawable.voucher);
MultipartBody.Builder builder = new MultipartBody.Builder()
.setType(MultipartBody.FORM);
for(int i = 0 ; i < moviesList.size(); i++) {
File file = new File(String.valueOf(moviesList.get(i)));
if(file.exists()){
final MediaType MEDIA_TYPE = MediaType.parse(String.valueOf(moviesList.get(i)));
builder.addFormDataPart("my_images[]",file.getName(),RequestBody.create(MEDIA_TYPE,file));
}
else {
Log.d("Hello", "file not exist ");
}
}
RequestBody requestBody = builder.build();
Request request = new Request.Builder()
.url(urls)
.post(requestBody)
.build();
OkHttpClient client = new OkHttpClient.Builder().build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.d("Response is :",String.valueOf(response));
Toast.makeText(getApplicationContext(),"Done",Toast.LENGTH_LONG).show();
}
});