Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploading multiple files using same name. #79

Open
Chuntianhong opened this issue Aug 3, 2018 · 3 comments
Open

Uploading multiple files using same name. #79

Chuntianhong opened this issue Aug 3, 2018 · 3 comments

Comments

@Chuntianhong
Copy link

I need to upload multiple files using same name.
How I can resolve this issue?
PHP already supports it. Please check following url.

Thank you for your answer.

@NainalChauhan
Copy link

NainalChauhan commented Sep 14, 2018

@Chuntianhong did you found any solution for sending array of files?

@Chuntianhong
Copy link
Author

Yes I found it.

I state my solution here
I used okhttp3 library.

//...
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okhttp3.Callback;
import okhttp3.Call;
//...

//--- Send reqeust for uploading the image ---//
MultipartBody.Builder builder = new MultipartBody.Builder();
builder.setType(MultipartBody.FORM);

    builder.addFormDataPart("item_title", itemTitle);
    builder.addFormDataPart("description", itemDesc);
    builder.addFormDataPart("price", itemPrice);

    // Add Image Data with name name
    for (int i = 0; i < mImagePaths.size(); i++) {
        File file = new File(mImagePaths.get(i));
        builder.addFormDataPart("product_image[]", file.getName(), 
        RequestBody.create(MediaType.parse("multipart/form-data"), file));
    }

    MultipartBody requestBody = builder.build();
    OkHttpClient client = new OkHttpClient();
    okhttp3.Request request = new okhttp3.Request.Builder()
            .url(MDApiType.getAddEditProductUrl())
            .addHeader("Accept", "application/json")
            .addHeader("Authorization", String.format("Bearer %s", mConfig.getEnt("AccessToken")))
            .post(requestBody)
            .build();
    
    //showProgressDialog();
    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
            //hideProgressDialog();
        }

        @Override
        public void onResponse(Call call, okhttp3.Response response) throws IOException {
            //hideProgressDialog();
            if (!response.isSuccessful()) {
                showAlert(R.string.error_select_item_type);
            } else {
                String responseString = response.message();
                responseString = response.body().string();
            }
        }
    });

.......

@echo30
Copy link

echo30 commented Sep 20, 2018

//SimpleMultiPartRequest Instance
SimpleMultiPartRequest smr = new SimpleMultiPartRequest(Request.Method.POST,
....
});

//Loop images
for(int index = 0; index < images.size(); index++) {
smr.addFile("uploaded_files["+index+"]", images.get(index));
}

//Queue and other codes
....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants