Last active
          May 24, 2019 07:05 
        
      - 
      
 - 
        
Save LiewJunTung/2d85f846c5459d6fed212a6401104f20 to your computer and use it in GitHub Desktop.  
    Upload with Kotlin Native
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | sealed class UploadFileType | |
| data class UploadFileForm( | |
| val key: String, | |
| val value: String, | |
| val filename: String = "file.jpg", | |
| val contentType: String = "image/jpeg" | |
| ) : UploadFileType() | |
| data class UploadForm( | |
| val key: String, | |
| val value: String | |
| ) : UploadFileType() | |
| fun multipartUpload(postUrl: String, bodies: ArrayList<UploadFileType>, headers: ArrayList<Pair<String, String>>?): String { | |
| val request = NSMutableURLRequest(NSURL.URLWithString(postUrl)!!) | |
| request.setHTTPMethod("POST") | |
| val boundary = "---------------------------14737809831466499882746641449" | |
| val contentType = "multipart/form-data; boundary=$boundary" | |
| request.addValue(contentType, "Content-Type") | |
| headers?.forEach { | |
| request.addValue(it.second, it.first) | |
| } | |
| val postBody: NSMutableData = NSMutableData.data() as NSMutableData | |
| val boundaryStr = "\r\n--$boundary\r\n" as NSString | |
| bodies.forEach { | |
| postBody.appendData(boundaryStr.dataUsingEncoding(NSUTF8StringEncoding)!!) | |
| when (it) { | |
| is UploadFileForm -> { | |
| val nsdata = NSData.create(contentsOfFile = it.fileUrl) ?: throw Exception("file not found!") | |
| val filestr = "Content-Disposition: form-data; name=\"${it.fileKey}\"; filename=\"${it.filename}\"\r\n" as NSString | |
| val constr = "Content-Type: ${it.contentType}\r\n\r\n" as NSString | |
| postBody.appendData(filestr.dataUsingEncoding(NSUTF8StringEncoding)!!) | |
| postBody.appendData(constr.dataUsingEncoding(NSUTF8StringEncoding)!!) | |
| postBody.appendData(NSData.dataWithData(nsdata)) | |
| } | |
| is UploadForm -> { | |
| val formStr = "Content-Disposition:form-data; name=\"${it.key}\"\r\n\r\n" as NSString | |
| val sfStr = it.value as NSString | |
| postBody.appendData(formStr.dataUsingEncoding(NSUTF8StringEncoding)!!) | |
| postBody.appendData(sfStr.dataUsingEncoding(NSUTF8StringEncoding)!!) | |
| } | |
| } | |
| } | |
| val endstr = "\r\n--$boundary--\r\n" as NSString | |
| postBody.appendData(endstr.dataUsingEncoding(NSUTF8StringEncoding)!!) | |
| println("length ${postBody.length}") | |
| request.setHTTPBody(postBody) | |
| val data = NSURLConnection.sendSynchronousRequest(request, null, null) | |
| val responseString = NSString.stringWithUTF8String(data!!.bytes!!.reinterpret()) | |
| return responseString ?: "None" | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment