[iOS/Swift] Alamofire๋กœ ์„œ๋ฒ„(FastAPI)์— ๋™์˜์ƒ ์—…๋กœ๋“œํ•˜๊ธฐ

2022. 5. 23. 20:32ยทiOS/Swift

์Šฌ์Šฌ ์บก๋””๊ฐ€ ๋๋‚˜๊ฐ„๋‹ค..

์‹œ๊ฐ„์ด ๋‚จ์•„์„œ ์‚ฝ์งˆ ์—„์ฒญํ–ˆ๋˜ ๋ถ€๋ถ„์„ ์ •๋ฆฌํ•ด๋ณด๋ ค๊ณ  ํ•œ๋‹ค.

 

Alamofire ๊นƒ ์ฃผ์†Œ

https://github.com/Alamofire/Alamofire

 

์ฒ˜์Œ์— ํ—ท๊ฐˆ๋ ธ๋˜๊ฒŒ ๋™์˜์ƒ์€ ์–ด๋–ค ํ˜•์‹์œผ๋กœ ์ €์žฅํ•ด์„œ ๋„˜๊ฒจ์•ผํ•˜๋Š”์ง€ ์ž˜ ๋ชฐ๋ž์—ˆ๋‹ค.

URLํ˜•์‹์œผ๋กœ ๋„˜๊ธฐ๋‹ˆ๊น ์ž˜ ๋„˜๊ฒจ์กŒ๋‹ค.

์ฐธ๊ณ ๋กœ ๋™์˜์ƒ์€ ์•จ๋ฒ”์—์„œ ๊ฐ€์ ธ์™€์„œ ์ €์žฅํ–ˆ๋‹ค. (์ด ๋ถ€๋ถ„ ์ฝ”๋“œ๋Š” ์ƒ๋žตํ•จ)

import Alamofire

class ViewController: UIViewController {

    //๋™์˜์ƒ ์ €์žฅ
    var videoURL: URL?

    //๋ฒ„ํŠผ ํด๋ฆญํ•˜๋ฉด ์„œ๋ฒ„๋กœ ๋™์˜์ƒ ์ „์†ก
        @IBAction func reportButtonClicked(_ sender: UIBarButtonItem) {
            do {
                print("videoURL : \(videoURL!)")
                uploadImage(imageURL: videoURL!)
            } catch {

            }
        }
    }
}
//๋™์˜์ƒ ์„œ๋ฒ„๋กœ ์ „๋‹ฌํ•ด์ฃผ๋Š” ์ฝ”๋“œ
func uploadImage(imageURL: URL?) {
        
        let URL = "http://127.0.0.1:8000/uploadfiles"
        let header : HTTPHeaders = [
            "Content-Type" : "application/json"]
    
    AF.upload(multipartFormData: { multipartFormData in
        multipartFormData.append(imageURL!, withName: "files", fileName: "video.mp4", mimeType: "video/mp4")
        
    }, to: URL, usingThreshold: UInt64.init(), method: .post, headers: header).response { response in
        guard let statusCode = response.response?.statusCode,
              statusCode == 200
        else { return }
    }
}

 

ํŒŒ์ด์ฌ ์ฝ”๋“œ

from fastapi import FastAPI, File, UploadFile
from typing import List
import os

app = FastAPI()

@app.get("/")
def read_root():
  return { "Hello": "World" }

@app.post("/uploadfiles")
async def create_upload_files(files: List[UploadFile] = File(...)):
    print('here')
    UPLOAD_DIRECTORY = "./"
    for file in files:
        contents = await file.read()
        with open(os.path.join(UPLOAD_DIRECTORY, file.filename), "wb") as fp:
            fp.write(contents)
        print(file.filename)
    return {"filenames": [file.filename for file in files]}

@app.post("/files/")
async def create_files(files: List[bytes] = File(...)):
    return {"file_sizes": [len(file) for file in files]}

 

'iOS > Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[iOS/Swift] Image ๋‹คํฌ๋ชจ๋“œ ๋Œ€์‘ํ•˜๊ธฐ  (0) 2022.11.23
[Swift] UI ๊ตฌํ˜„ ์‹œ Storyboard์™€ Code Base ๋น„๊ต  (0) 2022.10.26
[iOS/Swift] ์นด๋ฉ”๋ผ๋กœ ๋™์˜์ƒ ์ดฌ์˜ํ•˜๊ธฐ  (0) 2022.05.21
[iOS] Charts ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜๋ฐฉ๋ฒ• ๋ฐ ์‚ฌ์šฉ๋ฐฉ๋ฒ•  (0) 2022.03.03
[iOS] Swift ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ typing animation  (0) 2021.08.23
'iOS/Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • [iOS/Swift] Image ๋‹คํฌ๋ชจ๋“œ ๋Œ€์‘ํ•˜๊ธฐ
  • [Swift] UI ๊ตฌํ˜„ ์‹œ Storyboard์™€ Code Base ๋น„๊ต
  • [iOS/Swift] ์นด๋ฉ”๋ผ๋กœ ๋™์˜์ƒ ์ดฌ์˜ํ•˜๊ธฐ
  • [iOS] Charts ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์„ค์น˜๋ฐฉ๋ฒ• ๋ฐ ์‚ฌ์šฉ๋ฐฉ๋ฒ•
๐ŸฅญMango
๐ŸฅญMango
  • ๐ŸฅญMango
    AppleMango๐Ÿฅญ
    ๐ŸฅญMango
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ
      • iOS
        • Swift
        • SwiftUI
        • RxSwift
      • Algorithm
        • C
        • Python
        • Swift
      • Computer Science
        • ์ปดํ“จํ„ฐ ๋„คํŠธ์›Œํฌ
        • OS
      • ...
      • ๊ฐœ๋ฐœ ํƒ€์ž„์บก์А
        • Python
        • Flutter
        • Android
        • Kotlin
        • Java
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    ํ† ๋งˆํ† 
    MapMarker
    ํŒŒ์ด์ฌ ์ •๋ ฌ
    14503 ๋กœ๋ด‡์ฒญ์†Œ๊ธฐ
    Custom Map Marker
    1613 ํŒŒ์ด์ฌ
    Code Base
    14503 ํŒŒ์ด์ฌ
    ํŒŒ์ด์ฌ 14503
    ํŒŒ์ด์ฌ 6118
    ํŒŒ์ด์ฌ
    1613 ์—ญ์‚ฌ
    swiftUI tabview
    IOS
    1์ฐจ์› ๋ฟŒ์š”๋ฟŒ์š”
    ์—ญ์‚ฌ ํŒŒ์ด์ฌ
    swiftUI Gradients
    ๋ฐฑ์ค€ ์†Œ์ˆ˜
    ์Šคํƒ
    ์ฝ”ํ‹€๋ฆฐ ์ƒ์†
    ํŒŒ์ด์ฌ ํ† ๋งˆํ† 
    6118
    Apple Login
    typing animation
    ๋ฐฑ์ค€ ํ† ๋งˆํ† 
    ํŒŒ์ด์ฌ 1459
    Swift Hello World!
    ์ฝ”ํ‹€๋ฆฐ
    SwiftUI
    SwiftUI Apple Login
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.2
๐ŸฅญMango
[iOS/Swift] Alamofire๋กœ ์„œ๋ฒ„(FastAPI)์— ๋™์˜์ƒ ์—…๋กœ๋“œํ•˜๊ธฐ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”