Sending file from iOS to PHP using POST

后端 未结 3 532
慢半拍i
慢半拍i 2020-12-13 23:01

I\'m trying to send an audio file to a PHP server. Here is the Objective-C code:

NSData *data = [NSData dataWithContentsOfFile:filePath];

NSLog(@\"File Size         


        
3条回答
  •  生来不讨喜
    2020-12-13 23:12

    In swift you can post like this..

        func wsPostData(apiString: String, jsonData: String) -> Void
        {
             //apiString means base URL
            let postData = jsonData.dataUsingEncoding(NSASCIIStringEncoding, allowLossyConversion: true)
            let postLenth = "\(UInt((postData?.length)!))"
            let request = NSMutableURLRequest()
            request.URL = NSURL(string: apiString)
            request.HTTPMethod = "POST"
            request.setValue(postLenth, forHTTPHeaderField: "Content-Lenth")
            request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
            request.HTTPBody = postData
            NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) { (response: NSURLResponse?, data: NSData?, error:NSError?) in
    
                print("response\(response)")
                var dict:NSDictionary!
                do {
                    dict = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String: AnyObject]
                    print(" responce data is \(dict)")
                }
                catch let error as NSError
                {
                    print("Something went wrong\(error)")
                }
    
            }
        }
    

提交回复
热议问题