Uploading multipart file with Koush Ion library

前端 未结 2 474
抹茶落季
抹茶落季 2021-01-03 05:51

In my last app I\'m going to use Koush Ion library. It\'s so handy but i have a problem with uploading file to my rest server. note: My server response to success upload Pro

相关标签:
2条回答
  • 2021-01-03 06:16

    Searched pretty long for this and noticed that the important thing was to include POST in .load()

    Ion.with(getBaseContext()).load("POST",url).uploadProgressHandler(new ProgressCallback()
            {
                @Override
                public void onProgress(long uploaded, long total)
                {
                    System.out.println("uploaded " + (int)uploaded + " Total: "+total);
                }
            }).setMultipartParameter("platform", "android").setMultipartFile("image", new File(getPath(selectedImage))).asString().setCallback(new FutureCallback<String>()
            {
                @Override
                public void onCompleted(Exception e, String result)
                {
    
                }
            });
    
    0 讨论(0)
  • 2021-01-03 06:39

    I actually works for me, Here is my code:

    final File fileToUpload = new File(localFilePath);
    Ion.with(context)
                .load(Urls.UPLOAD_PICTURE)
                .uploadProgressHandler(new ProgressCallback() {
                    @Override
                    public void onProgress(long uploaded, long total) {
                        // Displays the progress bar for the first time.
                        mNotifyManager.notify(notificationId, mBuilder.build());
                        mBuilder.setProgress((int) total, (int) uploaded, false);
                    }
                })
                .setTimeout(60 * 60 * 1000)
                .setMultipartFile("upload", "image/jpeg", fileToUpload)
                .asJsonObject()
                        // run a callback on completion
                .setCallback(new FutureCallback<JsonObject>() {
                    @Override
                    public void onCompleted(Exception e, JsonObject result) {
                        // When the loop is finished, updates the notification
                        mBuilder.setContentText("Upload complete")
                                // Removes the progress bar
                                .setProgress(0, 0, false);
                        mNotifyManager.notify(notificationId, mBuilder.build());
                        if (e != null) {
                            Toast.makeText(context, "Error uploading file", Toast.LENGTH_LONG).show();
                            return;
                        }
                        Toast.makeText(context, "File upload complete", Toast.LENGTH_LONG).show();
                    }
                });
    }
    

    Hope it helps someone :)

    0 讨论(0)
提交回复
热议问题