如何在Swift服务器上上传视频和图像

时间:2016-04-18 07:45:55

标签: ios xcode swift

我正在尝试使用Json和php在swift上上传服务器上的视频和图像。我正在使用更多参数上传图像和视频。我可以在服务器上传图像,但不能上传视频。

 func myImageUploadRequest(){
    let myUrl = NSURL(string: "http://192.168.3.52/cobaupload2/index.php");

    let request = NSMutableURLRequest(URL:myUrl!);
    request.HTTPMethod = "POST";

    let param = [
        "firstName"  : "TESTNAME",
        "lastName"    : "TESTNAMELAST",
        "userId"    : "10"
    ]

    let boundary = generateBoundaryString()
    request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
    let imageData = UIImageJPEGRepresentation(myImageView.image!, 1)
    if(imageData==nil)  { return; }
    request.HTTPBody = createBodyWithParameters(param, filePathKey: "file", imageDataKey: imageData!, boundary: boundary)

    myActivityIndicator.startAnimating();
    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in

        if error != nil {
            print("error=\(error)", terminator: "")
            return
        }

        print("******* response = \(response)", terminator: "")
        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("****** response data = \(responseString!)")

        //let json:NSDictionary = (try! NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers )) as! NSDictionary
        dispatch_async(dispatch_get_main_queue(),{
            self.myActivityIndicator.stopAnimating()
            self.myImageView.image = nil;
        });
    }
    task.resume()
}

和php文件

<?php
if (move_uploaded_file($_FILES['file']['tmp_name'], "image.png")) {
echo "File uploaded: ".$_FILES["file"]["name"];
} 

任何人都可以教我视频上传。谢谢

1 个答案:

答案 0 :(得分:0)

图像和视频有什么区别?它们都是二进制数据。可能的原因可能是对上传文件大小的php限制。 看看this manual和你的php.ini。必须有行

Maximum allowed size for uploaded files.
upload_max_filesize = 10M

; Must be greater than or equal to upload_max_filesize
post_max_size = 10M