Alamofire: multipartFormData ์—…๋กœ๋“œ ์ง„ํ–‰๋ฅ ์„ ํ‘œ์‹œํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

์— ๋งŒ๋“  2017๋…„ 06์›” 27์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: Alamofire/Alamofire

์ผ๋ฐ˜ ์—…๋กœ๋“œ์— ๋Œ€ํ•ด ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

let fileURL = Bundle.main.url(forResource: "data", withExtension: "zip")

Alamofire.upload(fileURL!, to: "http://localhost:3000/upload")
    .uploadProgress { progress in // main queue by default
        print("Upload Progress: \(progress.fractionCompleted)")
    }
    .responseJSON { response in
        debugPrint(response)
}

๊ทธ๋Ÿฌ๋‚˜ multipartFormData ์—…๋กœ๋“œ ์ง„ํ–‰ ์ƒํ™ฉ์„ ์–ด๋–ป๊ฒŒ ํ‘œ์‹œํ•ด์•ผ ํ•ฉ๋‹ˆ๊นŒ?

Alamofire.upload(multipartFormData: { (multipartFormData) in
  let path = "\(Bundle.main.resourcePath!)/images"
  let all = try? FileManager.default.contentsOfDirectory(at: URL(string: path)!, includingPropertiesForKeys: nil, options: [])

  let filtered = all!.filter { $0.pathExtension == "jpg" }
  for item in filtered {
      let url = URL(fileURLWithPath: item.path, isDirectory: false)
      multipartFormData.append(url, withName: "photos", fileName: url.lastPathComponent, mimeType: "image/jpeg")
  }
}, to: "http://localhost:3000/upload") {
  (encodingResult) in
  switch encodingResult {
  case .success(let upload, _, _):
      upload.responseJSON { response in
          debugPrint(response)
      }
  case .failure(let encodingError):
      print(encodingError)
  }
}

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

Alamofire.upload(multipartFormData: { (multipartFormData) in
    ...
}, to: "http://localhost:3000/upload") {
  (encodingResult) in
  switch encodingResult {
  case .success(let upload, _, _):
      upload.uploadProgress(closure: { (progress) in
           print("Upload Progress: \(progress.fractionCompleted)")
      })
      upload.responseJSON { response in
          debugPrint(response)
      }
  case .failure(let encodingError):
      print(encodingError)
  }
} 

๋ชจ๋“  3 ๋Œ“๊ธ€

Alamofire.upload(multipartFormData: { (multipartFormData) in
    ...
}, to: "http://localhost:3000/upload") {
  (encodingResult) in
  switch encodingResult {
  case .success(let upload, _, _):
      upload.uploadProgress(closure: { (progress) in
           print("Upload Progress: \(progress.fractionCompleted)")
      })
      upload.responseJSON { response in
          debugPrint(response)
      }
  case .failure(let encodingError):
      print(encodingError)
  }
} 

์ •๋ง ๊ณ ๋ง™์Šต๋‹ˆ๋‹ค. ํšจ๊ณผ๊ฐ€์žˆ๋‹ค.
์–ด์จŒ๋“  ์ •์ƒ์ ์ธ http ์š”์ฒญ์˜ ์ง„ํ–‰ ์ƒํ™ฉ์„ ํ‘œ์‹œํ•˜๋ ค๋ฉด?

์ฃ„์†กํ•ฉ๋‹ˆ๋‹ค. ๋ฒ„๊ทธ ๋ณด๊ณ ์„œ ๋ฐ ๊ธฐ๋Šฅ ์š”์ฒญ์— GitHub ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์•ž์œผ๋กœ๋Š” ์Šคํƒ ์˜ค๋ฒ„ํ”Œ๋กœ ์—์„œ ์ด์™€ ๊ฐ™์€ ์งˆ๋ฌธ์„ ์—ด๊ณ  alamofire ํƒœ๊ทธ๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๊ฑด๋ฐฐ. ๐Ÿป


๊ธฐ์—ฌ ๊ฐ€์ด๋“œ๋ผ์ธ์—์„œ

์งˆ๋ฌธํ•˜๊ธฐ

GitHub๋ฅผ ์ง€์› ํฌ๋Ÿผ์œผ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ํ”„๋กœ์ ํŠธ ์ž์ฒด์™€ ๊ด€๋ จ์ด ์—†๋Š” ์‚ฌ์šฉ ๊ด€๋ จ ์งˆ๋ฌธ์€ ๋Œ€์‹  Stack Overflow ์— ๋ฌธ์˜ํ•˜์„ธ์š”. ๊ทธ๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ฌธ์ œ๋ฅผ ๋” ๋นจ๋ฆฌ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๊ณ  ๋™์ผํ•œ ์งˆ๋ฌธ์„ ๊ฐ€์ง„ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์ด ๋‹ต์„ ์ฐพ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ์œ ์ง€ ๊ด€๋ฆฌ์ž๋Š” ๋‹ค๋ฅธ ์‚ฌ๋žŒ์„ ์œ„ํ•ด ํ”„๋กœ์ ํŠธ๋ฅผ ๊ฐœ์„ ํ•˜๋Š” ๋ฐ ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰