2017-01-17 198 views
0

在我的應用程序中,我想上傳幾張圖片到服務器。問題是我無法上傳它。相同的代碼適用於服務器,我可以上傳圖片。但對於laravel服務器,我無法上傳它。圖片上傳到laravel服務器iOS

這我得到

<OS_dispatch_data: data[0x7fe220759040] = { leaf, size = 30, buf = 0x7fe22063cff0 }> 

Optional({"status_code":422,"status":"Failure","response":{"userId":["The user id field is required."],"propertyId":["The property id field is required."],"title":["The title field is required."],"description":["The description field is required."]},"message":"422 Unprocessable Entity","debug":{"line":22,"file":"\/var\/www\/html\/embassy\/vendor\/dingo\/api\/src\/Http\/FormRequest.php","class":"Dingo\\Api\\Exception\\ValidationHttpException","trace":["#0 \/var\/www\/html\/embassy\/vendor\/laravel\/framework\/src\/Illuminate\/Validation\/ValidatesWhenResolvedTrait.php(25): Dingo\\Api\\Http\\FormRequest->failedValidation 

的方式代碼

func sendImageWithParams(image:UIImage, url : String,photoParamKey:String,params : [String:String],quality:CGFloat=0.7,getResponse : Response) -> Void { 

     let request = NSMutableURLRequest(URL: NSURL(string: url)!); 
     request.HTTPMethod = "POST" 
     let boundary = generateBoundaryString() 
     request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") 
     let imageData = UIImageJPEGRepresentation(image, quality) 

     if(imageData==nil) { return; } 

     request.HTTPBody = createBodyWithParameters(params,photoParamKey:photoParamKey, imageDataKey: imageData!, boundary: boundary) 
     request.timeoutInterval = 20.0 

     let task = NSURLSession.sharedSession().dataTaskWithRequest(request, 
                    completionHandler: { 
                     (data, response, error) -> Void in 
                     if let data = data { 

                      getResponse(success: data, error: nil) 


                     } else if let error = error { 
                      getResponse(success: nil , error: error) 
                     } 
     }) 
     task.resume() 

    } 

    func generateBoundaryString() -> String { 
     return "Boundary-\(NSUUID().UUIDString)" 
    } 
    func createBodyWithParameters(parameters: [String: String]?,photoParamKey : String,imageDataKey: NSData, boundary: String) -> NSData { 

     let 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 mimetype = "image/jpg" 
     let filename = "image" 
     body.appendString("--\(boundary)\r\n") 
     body.appendString("Content-Disposition: form-data; name=\"\(photoParamKey)\"; 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 
    } 

} 

迴應,我現在用的功能

let p = ["title" : "a", 

         "propertyId":"1", 

         "description":"hey", 

         "userId":"131", 

         "params":"mobile" 

       ] 
NetworkRequest.sharedInstance.sendImageWithParams(imageView.image!, url: "http://52.66.131.92/api/blogs", photoParamKey: "photo", params: p, getResponse: { (success, error) in 
        print("Error:\(error)") 
        print("Success:\(success)") 
       }) 

PS - >我正確地發送所有參數!

+0

該錯誤看起來像你簡直沒有通過驗證。也許你發佈數據的方式。嘗試將它交換爲不同的形式,比如'form-data'或urlencoded',看看哪一個給你一個不同的錯誤。只是幫助你調試。 – EddyTheDove

+0

你可以發佈你的請求params? – Paras

+0

@Paras我編輯了我的文章,請看看。 –

回答

0

要麼你被擊中了錯誤的API端點或傳遞不正確參數,可以因爲在你的代碼,你要上傳的圖片,但API端點需要userIdpropertyIdtitledescription這是接受必要的參數圖片。我無法在您的請求中看到這些值。

+0

我正在傳遞正確的參數 –