SWIFT,用php上传视频

时间:2014-11-29 04:44:16

标签: php post swift upload server

它有效,但我想要的是发送视频(myvideo.mov)

有人帮我改变发送视频吗?

我尝试了几张图片并顺利进行,我不确定NSData会不会?

谢谢大家!

    let pathObject: String = "myImage.png"
let pathMyPhp = "http://myhost.com/etc/uploadFile.php"

let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
    let cDispositionName = "userfile"
    let contentDisposition = "Content-Disposition: form-data; name=\"\(cDispositionName)\"; filename=\"\(pathObject)\"\r\n"
    let mimeType = "application/octet-stream"
    let objUIImage = UIImage(named: pathObject)

    let fileExtension = pathObject.pathExtension
    var objNSData: NSData = NSData()

    let objNSData: NSData = UIImageJPEGRepresentation(objUIImage, 1.0);

    let pathPHP = NSURL(string:pathMyPhp)
    var err: NSError?
    let img = NSURL(string:pathObject)

    let boundary = "----------SwIfTeRhTtPrEqUeStBoUnDaRy"
    let contentType = "multipart/form-data; boundary=\(boundary)"

    let tempData = NSMutableData()
    tempData.appendData("--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    tempData.appendData(contentDisposition.dataUsingEncoding(NSUTF8StringEncoding)!)
    tempData.appendData("Content-Type: \(mimeType)\r\n\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)
    tempData.appendData(objNSData)
    tempData.appendData("\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

    var body = NSMutableData();
    body.appendData(tempData)
    body.appendData("\r\n--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding)!)

    var request = NSMutableURLRequest(URL: pathPHP!, cachePolicy: cachePolicy, timeoutInterval: 2.0)
    request.HTTPMethod = "POST"
    request.setValue(contentType, forHTTPHeaderField:"Content-Type")
    request.setValue("\(body.length)", forHTTPHeaderField: "Content-Length")
    request.HTTPBody = body

    var vl_error :NSErrorPointer = nil
    var responseData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error:vl_error)

    var results = NSString(data:responseData!, encoding:NSUTF8StringEncoding)
    println("\(results)")

非常感谢大家,我将不胜感激。

0 个答案:

没有答案