5

So I'm uploading a file from swift to a PHP server, the POST request arrives as expected with headers and all but I'm unable to get anything out of $_FILES. It's just an empty array.

I'm clearly doing something wrong on the Swift side, this is my code:

func testUpload(){
    let bundle = NSBundle.mainBundle()
    let path = bundle.pathForResource("someTestFile", ofType: "zip")!
    var data: NSData = NSData(contentsOfFile: path)!

    var request = NSMutableURLRequest(URL: NSURL(string: "http://testsite.com/upload")!)
    request.HTTPMethod = "POST"

    let boundary = "----------ds2Gry67efddagdfsfhsHF"
    let contentType = "multipart/form-data; boundary=\(boundary)"
    request.setValue(contentType, forHTTPHeaderField:"Content-Type")
    request.setValue("Keep-Alive", forHTTPHeaderField: "Connection")
    self.uploadFiles(request, data: data)
}

func uploadFiles(request: NSURLRequest, data: NSData) {
    var configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
    var session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
    var task = session.uploadTaskWithRequest(request, fromData: data)
    task.resume()
}

I'm pretty sure I'm missing something, I just can't figure out what it is...

6
  • Maybe something to do with contentDisposition? Commented Apr 20, 2015 at 14:25
  • have you consider using Alamofire instead? Commented Apr 20, 2015 at 14:54
  • see this very good answer stackoverflow.com/questions/26162616/… Commented Apr 20, 2015 at 14:57
  • What type of file are you sending? Are you trying to send anything else with the file? Have you attempted a var_dump of the $_FILE and $_POST arrays? Commented Apr 21, 2015 at 3:31
  • @jkaufman It's just a zip file, and not sending anything else apart from some headers, so $_POST is also empty Commented Apr 21, 2015 at 7:31

1 Answer 1

-1

POST method ,you need add header application/x-www-form-urlencoded

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.