์ผ๋ฐ ์ ๋ก๋์ ๋ํด ๋ค์์ ์ํํ ์ ์๋ค๋ ๊ฒ์ ์๊ณ ์์์ต๋๋ค.
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)
}
}
์ ๋ง ๊ณ ๋ง์ต๋๋ค. ํจ๊ณผ๊ฐ์๋ค.
์ด์จ๋ ์ ์์ ์ธ http ์์ฒญ์ ์งํ ์ํฉ์ ํ์ํ๋ ค๋ฉด?
์ฃ์กํฉ๋๋ค. ๋ฒ๊ทธ ๋ณด๊ณ ์ ๋ฐ ๊ธฐ๋ฅ ์์ฒญ์ GitHub ํ๋ก์ ํธ๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์์ผ๋ก๋ ์คํ ์ค๋ฒํ๋ก ์์ ์ด์ ๊ฐ์ ์ง๋ฌธ์ ์ด๊ณ alamofire
ํ๊ทธ๋ฅผ ์ง์ ํด์ผ ํฉ๋๋ค.
๊ฑด๋ฐฐ. ๐ป
GitHub๋ฅผ ์ง์ ํฌ๋ผ์ผ๋ก ์ฌ์ฉํ์ง ์์ต๋๋ค. ํ๋ก์ ํธ ์์ฒด์ ๊ด๋ จ์ด ์๋ ์ฌ์ฉ ๊ด๋ จ ์ง๋ฌธ์ ๋์ Stack Overflow ์ ๋ฌธ์ํ์ธ์. ๊ทธ๋ ๊ฒ ํ๋ฉด ๋ฌธ์ ๋ฅผ ๋ ๋นจ๋ฆฌ ํด๊ฒฐํ ์ ์๊ณ ๋์ผํ ์ง๋ฌธ์ ๊ฐ์ง ๋ค๋ฅธ ์ฌ๋์ด ๋ต์ ์ฐพ์ ์ ์์ต๋๋ค. ์ด๋ฅผ ํตํด ์ ์ง ๊ด๋ฆฌ์๋ ๋ค๋ฅธ ์ฌ๋์ ์ํด ํ๋ก์ ํธ๋ฅผ ๊ฐ์ ํ๋ ๋ฐ ์ง์คํ ์ ์์ต๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ