2015-09-15 79 views
1

到目前爲止,我一直在使用以下代碼將上傳圖片數據上傳到服務器。使用Alamofire上傳圖片以及其他字符串數據

Alamofire.upload(.POST, 
    URLString: "https://example.com/api/v1/users/profile_pic.json?auth_token=\(auth_token)", 
    multipartFormData: { multipartFormData in 
     multipartFormData.appendBodyPart(data: imageData, name: "avatar", fileName: "avatar_img.png", mimeType: "image/png") 
    }, 
    encodingCompletion: { encodingResult in 

     switch encodingResult { 
      case .Success (let upload, _, _): 
       upload.responseJSON { request, response, data, error in 

       // Do whatever 

       } 
      case .Failure (let encodingError): 
     } 

    }) 

但是現在我還需要將我的用戶的其他詳細信息上傳到服務器,例如first_name和last_name以及它。我應該如何在一個請求中與圖片數據一起執行此操作?

回答

0

這是你如何能做到這

Alamofire.upload(.POST, path, headers: self.predefinedHeaders, multipartFormData: { multipartFormData in 

        multipartFormData.appendBodyPart(data: imageData, name: "avatar", fileName: "avatar_img.png", mimeType: "image/png") 

        for (key, value) in parameters 
        { 
          multipartFormData.appendBodyPart(data: value.dataUsingEncoding(NSUTF8StringEncoding)!, name: key) 
        } 

        }, encodingCompletion:{ encodingResult in 
        //your handling 
        }) 
相關問題