2017-06-01 31 views
1

您可以幫助我上傳帶有參數和請求驗證的圖片MultipartFormData如何使用multipartFormData和參數上傳圖片,請求使用Alamofire進行驗證4.3

let username = "username" 
let password = "password" 

的請求認證

let kurl = "Server url" 
    let headers: HTTPHeaders = ["Authorization":"Basic \(base64EncodedCredential)"] 
    let parmeter = ["name":"taykun","is_user":"1"] 

     Alamofire.upload(multipartFormData: { (multipartFormData) in 

        }, to: kurl, encodingCompletion: { (result) in 

        }) 

回答

0

試用alamofire驗證碼:

func uploadImageWithData(strApiUrl:String,strImageUrl:String,param:Dictionary<String, String>? = nil ,completionHandler: @escaping (_ result: NSDictionary) -> Void) -> Void{ 
      Alamofire.upload(multipartFormData: { multipartFormData in 
       let urlImage:URL = URL.init(fileURLWithPath: strImageUrl) 
       multipartFormData.append(urlImage, withName: "your key for image use in API") 
       for (key, value) in param! { 
        multipartFormData.append(value.data(using:String.Encoding(rawValue: String.Encoding.utf8.rawValue))!, withName: key) 
       } 
      }, to: strApiUrl, encodingCompletion: { 
       (encodingResult) in 
       print("encoding result:\(encodingResult)") 
       switch encodingResult { 
       case .success(let upload, _, _): 
        upload.uploadProgress(closure: { (Progress) in 
         print("Upload Progress: \(Progress.fractionCompleted)") 
         //send progress using delegate 
        }) 
        upload.responseJSON{ (response) in     } 
       case .failure(let encodingError): 
        print(encodingError) 
       } 
      }) 

     } 


    func returnResponse (response: DataResponse<Any>)->NSDictionary 
     { 
      if (response.result.isSuccess) 
      { 
       if let value = response.result.value 
       { 
        return value as! NSDictionary 
       } 
      } 
      else 
      { 
       print("\(response.error?.localizedDescription)") 
       var statusCode = 0 
       if let error = response.result.error as? AFError { 
        statusCode = error._code // statusCode private 
        switch error { 
        case .invalidURL(let url): 
         print("Invalid URL: \(url) - \(error.localizedDescription)") 
        case .parameterEncodingFailed(let reason): 
         print("Parameter encoding failed: \(error.localizedDescription)") 
         print("Failure Reason: \(reason)") 
        case .multipartEncodingFailed(let reason): 
         print("Multipart encoding failed: \(error.localizedDescription)") 
         print("Failure Reason: \(reason)") 
        case .responseValidationFailed(let reason): 
         print("Response validation failed: \(error.localizedDescription)") 
         print("Failure Reason: \(reason)") 

         switch reason { 
         case .dataFileNil, .dataFileReadFailed: 
          print("Downloaded file could not be read") 
         case .missingContentType(let acceptableContentTypes): 
          print("Content Type Missing: \(acceptableContentTypes)") 
         case .unacceptableContentType(let acceptableContentTypes, let responseContentType): 
          print("Response content type: \(responseContentType) was unacceptable: \(acceptableContentTypes)") 
         case .unacceptableStatusCode(let code): 
          print("Response status code was unacceptable: \(code)") 
          statusCode = code 
         } 
        case .responseSerializationFailed(let reason): 
         print("Response serialization failed: \(error.localizedDescription)") 
         print("Failure Reason: \(reason)") 
         // statusCode = 3840 ???? maybe.. 
        } 

        print("Underlying error: \(error.underlyingError)") 
       } else if let error = response.result.error as? URLError { 
        print("URLError occurred: \(error)") 
       }else if let error = response.result.error as? NSError{ 
        print("timeout") 
        statusCode = error._code 
       }else { 
        print("Unknown error: \(response.result.error)") 
       } 

       print(statusCode) 
       //make a response with nil value and set error or other information in it and return it. 
       let paramDic:NSMutableDictionary = NSMutableDictionary() 
       paramDic[KEY_MESSAGE] = response.error?.localizedDescription 
       paramDic[KEY_DATA] = nil 
       paramDic[KEY_STATUS_CODE] = "\(statusCode)" 
       paramDic[KEY_SUCCESS] = 0 

       return paramDic as NSDictionary 
      } 
      return NSDictionary() 
     } 

調用此方法是這樣的:

let imgUrl = //first store your image locally and then set image url 
var dicParam = Dictionary<String , String>()//your requestparameter dictionary 
     API.uploadImageWithData(strApiUrl: webserviceurl, strImageUrl: imgUrl, param: dicParam, completionHandler: { (result) in 
      print(result) 
      if result[KEY_SUCCESS] as! Int == 1{ 
      //success 
      }else{ 
      //fail 
      } 
     }) 
+0

請求的URL需要像 讓利基本身份驗證用戶名= 「username」 let password =「password」 我怎麼能通過這個? – Hitesh

+0

不需要這樣做。嘗試我的代碼 –

+0

簡單的製作參數字典並傳遞給func就好像var dicParam = Dictionary () dicParam [「uuername」] =「name」 dicParam [「password」] =「password」 –

相關問題