5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Moyaを使ってmultipart/form-dataでimageのPOST

Posted at

今更ながら若干ハマった為、メモ

var task: Task {
        switch self {
              
        case .hoge(let files):
        //filesは[Data]

            let fileMFDataArray: [MultipartFormData] = files.enumerated().map({ (index, data) in
                MultipartFormData(provider: .data(data),
                                  name: "file", //こっちがKey
                                  fileName: "\(Int(Date().timeIntervalSince1970))\(index)", //UnixTime + index番号 のファイル名を生成
                                  mimeType: "multipart/form-data")
            })
            return .uploadMultipart(fileMFDataArray)
    
        }
    }

あとは忘れずにheaderを["Content-Type": "multipart/form-data"]にすれば完了!

5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?