Advertisement
Vassa007

upload

Aug 17th, 2021
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.99 KB | None | 0 0
  1.  
  2.     private fun uploadNilai(path: Uri?) {
  3.         try {
  4.  
  5.             Log.e(TAG, "onActivityResult: ${context?.let { path?.let { it1 -> fileFromContentUri(it, it1) } }}" )
  6.             val mFile = RequestBody.create(MediaType.parse("application/vnd.ms-excel"), context?.let { path?.let { it1 -> fileFromContentUri(it, it1) } })
  7.             val excelUpload = MultipartBody.Part.createFormData("template", context?.let { FileUtils.getFileName(it, path) }, mFile)
  8.             val idmatakuliah = RequestBody.create(MediaType.parse("text/plain"), idmatakuliah)
  9.             val idstafdosen = RequestBody.create(MediaType.parse("text/plain"), runBlocking { userPrefrences.idStafDosenFlow.first() })
  10.  
  11.  
  12.             viewModel.uploadNilaiModel(idmatakuliah, idstafdosen, excelUpload)
  13.             val progressDialogManager = context?.let { it -> ProgressDialogManager(it).loader("Sedang mengirim data") }
  14.             viewModel.uploadNilaiResponse.observe(viewLifecycleOwner, {
  15.                 when (it) {
  16.  
  17.                     is Resource.Loading -> progressDialogManager?.show()
  18.                     is Resource.Success -> {
  19.                         val responseModel: ResponseModel = it.value
  20.                         val decrypt = EncDec().toDerypt(responseModel.streams)
  21.                         Log.e("TAG", "upload : $decrypt")
  22.  
  23.  
  24.                         progressDialogManager?.dismiss()
  25.                     }
  26.  
  27.                     is Resource.Failure -> {
  28.                         progressDialogManager?.dismiss()
  29. //                        handleApiError(it)
  30.                         Log.e(TAG, "uploadNilai: $it")
  31.                     }
  32.                 }
  33.             })
  34.  
  35.         } catch (e: Exception){
  36.             e.printStackTrace()
  37.         }
  38.     }
  39.  
  40.     fun fileFromContentUri(context: Context, contentUri: Uri): File {
  41.         // Preparing Temp file name
  42.         val fileExtension = getFileExtension(context, contentUri)
  43.         val fileName = "temp_file"+ if (fileExtension != null) ".$fileExtension" else ""
  44.  
  45.         // Creating Temp file
  46.         val tempFile = File(context.cacheDir, fileName)
  47.         tempFile.createNewFile()
  48.  
  49.         try {
  50.             val oStream = FileOutputStream(tempFile)
  51.             val inputStream = context.contentResolver.openInputStream(contentUri)
  52.  
  53.             inputStream?.let {
  54.                 copy(inputStream, oStream)
  55.             }
  56.  
  57.             oStream.flush()
  58.         } catch (e: Exception) {
  59.             e.printStackTrace()
  60.         }
  61.  
  62.         return tempFile
  63.     }
  64.  
  65.     private fun getFileExtension(context: Context, uri: Uri): String? {
  66.         val fileType: String? = context.contentResolver.getType(uri)
  67.         return MimeTypeMap.getSingleton().getExtensionFromMimeType(fileType)
  68.     }
  69.  
  70.     @Throws(IOException::class)
  71.     private fun copy(source: InputStream, target: OutputStream) {
  72.         val buf = ByteArray(8192)
  73.         var length: Int
  74.         while (source.read(buf).also { length = it } > 0) {
  75.             target.write(buf, 0, length)
  76.         }
  77.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement