2016-07-23 18 views
0

Ich folgte ein Tutorial online, um Bild von Swift Bild auf Server zu laden. Ich benutze sein Beispiel-Interface und alles sieht gut aus, außer ich bekomme Fehler "Nachricht": "Leider gab es einen Fehler beim Hochladen Ihrer Datei.", "Status": Fehler, "userId": "9". Meine Codes sindUpload Bild Swift über PHP zu Server

<?php 
    $firstName = $_POST["firstName"]; 
    $lastName = $_POST["lastName"]; 
    $userId = $_POST["userId"]; 

    $target_dir = "percyteng.com/orbit/ios/image";if(!file_exists($target_dir)) 
    { 
    mkdir($target_dir, 0777, true); 
    } 

    $target_dir = $target_dir . "/" . $_FILES["file"]["name"]; 
    echo json_encode($target_dir); 

    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_dir)) { 
    echo json_encode([ 
    "Message" => "The file ". basename($_FILES["file"]["name"]). " has been uploaded.", 
    "Status" => "OK", 
    "userId" => $_REQUEST["userId"] 
    ]); 
    } else { 

    echo json_encode([ 
    "Message" => "Sorry, there was an error uploading your file.", 
    "Status" => "Error", 
    "userId" => $_REQUEST["userId"] 
    ]); 

    } 
    ?> 

Ich habe überprüft und die Parameter wurden von swift übergeben. Hier ist mein Code in swift.

import UIKit 

    class ViewController: UIViewController, UIPickerViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate{ 


    @IBOutlet weak var image: UIImageView! 
    @IBOutlet weak var myActivityIndicator: UIActivityIndicatorView! 

     override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     self.view.endEditing(true) 
    } 

    @IBAction func selectImage(sender: AnyObject) { 
     let ImagePicker = UIImagePickerController() 
     ImagePicker.delegate = self 
     ImagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary 

     self.presentViewController(ImagePicker, animated: true, completion: nil) 
    } 
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) { 

     image.image = info[UIImagePickerControllerOriginalImage] as? UIImage 
     self.dismissViewControllerAnimated(true, completion: nil) 

    } 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

    } 

    @IBAction func Upload(sender: AnyObject) { 
     myImageUploadRequest() 
    } 
    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 
    func myImageUploadRequest() 
    { 

     let myUrl = NSURL(string: "http://www.percyteng.com/orbit/ios/try.php"); 
     //let myUrl = NSURL(string: "http://www.boredwear.com/utils/postImage.php"); 

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

     let param = [ 
      "firstName" : "Sergey", 
      "lastName" : "Kargopolov", 
      "userId" : "9" 
     ] 

     let boundary = generateBoundaryString() 

     request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") 


     let imageData = UIImageJPEGRepresentation(image.image!, 1) 

     if(imageData==nil) { return; } 

     request.HTTPBody = createBodyWithParameters(param, filePathKey: "file", imageDataKey: imageData!, boundary: boundary) 



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

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

      // You can print out response object 
      print("******* response = \(response)") 

      // Print out reponse body 
      let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding) 
      print("****** response data = \(responseString!)") 

      do { 
       if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary { 
        print(jsonResult) 
       } 
      } catch let error as NSError { 
       print(error.localizedDescription) 
      } 


      dispatch_async(dispatch_get_main_queue(),{ 
//    self.myActivityIndicator.stopAnimating() 
       self.image.image = nil; 
      }); 

      /* 
      if let parseJSON = json { 
      var firstNameValue = parseJSON["firstName"] as? String 
      println("firstNameValue: \(firstNameValue)") 
      } 
      */ 

     } 

     task.resume() 

    } 


    func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData { 
     var body = NSMutableData(); 

     if parameters != nil { 
      for (key, value) in parameters! { 
       body.appendString("--\(boundary)\r\n") 
       body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n") 
       body.appendString("\(value)\r\n") 
      } 
     } 

     let filename = "user-profile.jpg" 

     let mimetype = "image/jpg" 

     body.appendString("--\(boundary)\r\n") 
     body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n") 
     body.appendString("Content-Type: \(mimetype)\r\n\r\n") 
     body.appendData(imageDataKey) 
     body.appendString("\r\n") 



     body.appendString("--\(boundary)--\r\n") 

     return body 
    } 




    func generateBoundaryString() -> String { 
     return "Boundary-\(NSUUID().UUIDString)" 
    } 



    } 


    extension NSMutableData { 

    func appendString(string: String) { 
     let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) 
     appendData(data!) 
    } 
    } 

Antwort

0

Ihr iOS-Code funktioniert für mich, wenn ich move_uploaded_file() im PHP-Skript verwenden, um das Bild aus dem tempfile Pfad zu einem vorhandenen Verzeichnis auf meinem Server zu verschieben, so dass ich die dir laufe nicht Probleme in der Schaffung von (Ich habe auch die json-Konvertierung der Antwort auskommentiert). Daher wurden die Grenzen im Hauptteil der Anforderung alle korrekt erstellt.

Ich vermute, move_uploaded_file() schlägt aufgrund eines Verzeichnisproblems fehl. Versuchen Sie etwas wie folgt:

if(!file_exists($target_dir)) 
{ 
    mkdir($target_dir, 0777, true) or exit("Couldn't create $target_dir!"); 

} 
+0

Ja, ich habe mein Problem vor einer Weile herausgefunden. Ich musste nur mein traget_dir wie "image" schreiben. Danke tho –