Uploading file in php server from android device

前端 未结 7 1882
孤独总比滥情好
孤独总比滥情好 2020-11-27 02:34

I am trying to upload file to a php server from my android device but server is not receiving any file. here is my sample code, I found it on-line. php server , I am uploa

7条回答
  •  隐瞒了意图╮
    2020-11-27 03:04

    Those who are looking for Kotlin solution, can use this utility class. Here is the detailed article I have written on the topic. It uses OKHTTP3. Upload File to Server in Android Kotlin

    package com.mvp.handyopinion
    
    import android.app.Activity
    import android.app.ProgressDialog
    import android.net.Uri
    import android.util.Log
    import android.webkit.MimeTypeMap
    import android.widget.Toast
    import okhttp3.*
    import okhttp3.MediaType.Companion.toMediaTypeOrNull
    import okhttp3.RequestBody.Companion.asRequestBody
    import java.io.File
    
    class UploadUtility(activity: Activity) {
    
        var activity = activity;
        var dialog: ProgressDialog? = null
        var serverURL: String = "https://handyopinion.com/tutorials/UploadToServer.php"
        var serverUploadDirectoryPath: String = "https://handyopinion.com/tutorials/uploads/"
        val client = OkHttpClient()
    
        fun uploadFile(sourceFilePath: String, uploadedFileName: String? = null) {
            uploadFile(File(sourceFilePath), uploadedFileName)
        }
    
        fun uploadFile(sourceFileUri: Uri, uploadedFileName: String? = null) {
            val pathFromUri = URIPathHelper().getPath(activity,sourceFileUri)
            uploadFile(File(pathFromUri), uploadedFileName)
        }
    
        fun uploadFile(sourceFile: File, uploadedFileName: String? = null) {
            Thread {
                val mimeType = getMimeType(sourceFile);
                if (mimeType == null) {
                    Log.e("file error", "Not able to get mime type")
                    return@Thread
                }
                val fileName: String = if (uploadedFileName == null)  sourceFile.name else uploadedFileName
                toggleProgressDialog(true)
                try {
                    val requestBody: RequestBody =
                        MultipartBody.Builder().setType(MultipartBody.FORM)
                            .addFormDataPart("uploaded_file", fileName,sourceFile.asRequestBody(mimeType.toMediaTypeOrNull()))
                            .build()
    
                    val request: Request = Request.Builder().url(serverURL).post(requestBody).build()
    
                    val response: Response = client.newCall(request).execute()
    
                    if (response.isSuccessful) {
                        Log.d("File upload","success, path: $serverUploadDirectoryPath$fileName")
                        showToast("File uploaded successfully at $serverUploadDirectoryPath$fileName")
                    } else {
                        Log.e("File upload", "failed")
                        showToast("File uploading failed")
                    }
                } catch (ex: Exception) {
                    ex.printStackTrace()
                    Log.e("File upload", "failed")
                    showToast("File uploading failed")
                }
                toggleProgressDialog(false)
            }.start()
        }
    
        // url = file path or whatever suitable URL you want.
        fun getMimeType(file: File): String? {
            var type: String? = null
            val extension = MimeTypeMap.getFileExtensionFromUrl(file.path)
            if (extension != null) {
                type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
            }
            return type
        }
    
        fun showToast(message: String) {
            activity.runOnUiThread {
                Toast.makeText( activity, message, Toast.LENGTH_LONG ).show()
            }
        }
    
        fun toggleProgressDialog(show: Boolean) {
            activity.runOnUiThread {
                if (show) {
                    dialog = ProgressDialog.show(activity, "", "Uploading file...", true);
                } else {
                    dialog?.dismiss();
                }
            }
        }
    
    }
    

提交回复
热议问题