0
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 1 year has passed since last update.

大分前に書いたコードだけどQiitaみたら誰も記事にしてなかったので、参考になればと思い記事にしてみました。

自分は以下の様に実装してファイルアップロードを対応しました。多分同じところではまってる人はこれみれば何をすればいいかわかると思いますし、興味ない人は細かく書いてもみないと思うので、自分が実装したコードを載せておきます。

ファイルフォーマットが変わっても対象箇所を同じ様に設定してあげればアップロードされるとこまで確認してます。

import Moya
import Foundation
import UIKit

extension Api {
    struct UploadFiles: ApiTargetType {
        typealias Response = ApiResponse.File
        
        var method: Moya.Method {
            return .post
        }
        var path: String {
            let pathString = "resource_files"
            return pathString
        }
        
        var task: Task {
            let formData: [Moya.MultipartFormData] = [Moya.MultipartFormData(provider: .data(fileData), name: "file", fileName: self.fileName, mimeType: self.mimeType)]
            return .uploadMultipart(formData)
        }
        
        
        var fileData: Data
        var fileName: String
        var mimeType: String
        
        init(fileData: Data, fileName: String = "name", mimeType: String = "text/plain") {
            self.fileData = fileData
            self.fileName = fileName
            self.mimeType = mimeType
        }
    }
}
0
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
0
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?