PDA

View Full Version : اپلود فایل همراه با progress



samiasoft
یک شنبه 28 مرداد 1397, 00:38 صبح
سلام دوستان

همانطور که میدانید در کتابخانه volley امکان اپلود فایل میسر نبود و فقط میتونستیم تصویر رو به صورت رشته به سرور بفرستیم و....

اما من امروز از داخل سایت زیر یک کلاس دیدم که با استفاده از کتابخانه apache این امکان رو به کتابخانه والی میده که فایل هم بتونیم سمت سرور بفرستیم


http://findnerd.com/list/view/Upload-files-to-server-using-Volley-Library-as-a-HTTP-multipart-request/21439/


مساله ای که نتونستم حلش کنم این هستش که چطوری امکان نمایش درصد اپلود فایل رو هم داخل pregress نمایش بدیم ؟
داخل سایت های زیادی جستجو کردم اما راه حلی پیدانکردم.

samiasoft
یک شنبه 28 مرداد 1397, 03:43 صبح
برای کتابخانه رتروفیت هم از این کلاس جهت progress استفاده کردم اما متاسفانه به درستی درصد اپلود رو نشون نمیده !

public class ProgressRequestBody extends RequestBody { private File mFile;
private String mPath;
private UploadCallbacks mListener;


private static final int DEFAULT_BUFFER_SIZE = 2048;


public interface UploadCallbacks {
void onProgressUpdate(int percentage);
void onError();
void onFinish();
}


public ProgressRequestBody(final File file, final UploadCallbacks listener) {
mFile = file;
mListener = listener;
}


@Override
public MediaType contentType() {


String content_type=getMimeType(mFile.getPath());
return MediaType.parse(content_type);
}


@Override
public long contentLength() throws IOException {
return mFile.length();
}


@Override
public void writeTo(BufferedSink sink) throws IOException {
long fileLength = mFile.length();
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
FileInputStream in = new FileInputStream(mFile);
long uploaded = 0;


try {
int read;
Handler handler = new Handler(Looper.getMainLooper());


handler.post(new ProgressUpdater(0L, fileLength));
while (true) {


read = in.read(buffer);


if (read == -1) {
break;
}
sink.write(buffer, 0, read);
uploaded += read;
handler.post(new ProgressUpdater(uploaded, fileLength));


}
} finally {
sink.flush();
sink.close();
in.close();
}
}


private class ProgressUpdater implements Runnable {
private long mUploaded;
private long mTotal;
public ProgressUpdater(long uploaded, long total) {
mUploaded = uploaded;
mTotal = total;
}


@Override
public void run() {
mListener.onProgressUpdate((int)(100 * mUploaded / mTotal));
}
}


private String getMimeType(String patch) {


String extension = MimeTypeMap.getFileExtensionFromUrl(patch);
return extension;
}
}

و اکتیوتی اصلی :
public class uploadfile_Activity extends AppCompatActivity implements ProgressRequestBody.UploadCallbacks {

EditText txt_file_patch;
ProgressDialog progressBar;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_uploadfile);


txt_file_patch = findViewById(R.id.txt_file_pacth);


progressBar = new ProgressDialog(this);
progressBar.setMessage("در حال دانلود فایل");
progressBar.setIndeterminate(true);
progressBar.setProgressStyle(ProgressDialog.STYLE_ HORIZONTAL);
progressBar.setCancelable(true);
}


public void btn_click(View v) {


if (v.getId()==R.id.btn_upload){


upload();
}
}






private void upload(){


//show_progress();
progressBar.show();

String file_patch = txt_file_patch.getText().toString();
String file_name=file_patch.substring(file_patch.lastInde xOf("/")+1);
String content_type=getMimeType(file_patch);


ApiInterface_upload apiInterface = ApiClient_upload.getClient().create(ApiInterface_u pload.class);

ProgressRequestBody requestFile = new ProgressRequestBody(new File(file_patch), this);


MultipartBody.Part body =MultipartBody.Part.createFormData("file", file_name, requestFile);


Call<ResponseBody> call=apiInterface.upload(body);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {


try {
JSONObject jsonObject=new JSONObject(response.body().string());
String code=jsonObject.getString("response");


if(code.equals("ok")){


progressBar.dismiss();
Toast("با موفقیت اپلود شد");


}
else if(code.equals("error")){
progressBar.dismiss();

}


}
catch (Exception e) {
e.printStackTrace();

progressBar.dismiss();
}
}


@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("Tag","errorrrrrrrr"+t);

progressBar.dismiss();
}
});






}





@Override
public void onProgressUpdate(int percentage) {


progressBar.setIndeterminate(false);
progressBar.setProgress(percentage);
progressBar.setMax(100);


}


@Override
public void onError() {


}


@Override
public void onFinish() {
progressBar.setProgress(100);
}
}