How to upload .pdf .doc .txt files to server(mysql database) using Android

前端 未结 2 968
花落未央
花落未央 2021-01-29 15:01

I\'m facing some difficulties while uploading files to server, I need to know full code of file uploading with namevaluepair. Hear is my android code, I\'m getting only my file

相关标签:
2条回答
  • 2021-01-29 15:44

    If you want simple and fast then i will suggest you ION Library

    Post multipart/form-data and read JSON with an upload progress bar With ION :

    Ion.with(getContext())
    .load("https://koush.clockworkmod.com/test/echo")
    .uploadProgressBar(uploadProgressBar)
    .setMultipartParameter("goop", "noop")
    .setMultipartFile("archive", "application/zip", new File("/sdcard/filename.zip"))
    .asJsonObject()
    .setCallback(...)
    
    0 讨论(0)
  • 2021-01-29 15:50

    Use Retrofil or Okhttp.

    public static final String MULTIPART_FORM_DATA = "multipart/form-data";
    
     @NonNull
     private RequestBody createPartFromString(String descriptionString) {  
       return RequestBody.create(
            MediaType.parse(MULTIPART_FORM_DATA), descriptionString);
     }
    
     @NonNull
     private MultipartBody.Part prepareFilePart(String partName, Uri fileUri) {  
    // https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
    // use the FileUtils to get the actual file by uri
    File file = FileUtils.getFile(this, fileUri);
    
    // create RequestBody instance from file
    RequestBody requestFile =
        RequestBody.create(MediaType.parse(MULTIPART_FORM_DATA), file);
    
    // MultipartBody.Part is used to send also the actual file name
    return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
    }
    
    0 讨论(0)
提交回复
热议问题