如何在 Swift 中使用 Siesta Framework 上传文件?

How do I upload a file using Siesta Framework in Swift?

问题很简单:
我需要将 .zip 文件上传到服务器,如何使用 Siesta Framework 在 Swift 中执行此操作?

谢谢。

根据the GitHub page of Siesta,它目前不支持文件upload/download任务。看这张对比图:

其实上面的回答并不完全正确。 由于 Siesta 长期以来一直支持 POST 请求,您可以按照以下方式在 YourAPI class 中上传文件(内容类型:multipart/form-data)(PNG 或 JPEG 文件上传示例):

class YourAPI {

static let sharedInstance = YourAPI()

private let service = Service(baseURL: "http://your_server/rest/service", standardTransformers: [.text, .image])

private init() {
    // your init code
}

public enum ImageType {
    case png
    case jpeg
}

func generateBoundaryString() -> String {
    return "Boundary-\(UUID().uuidString)"
}

func constructHttpBodyForImageUpload(withBoundary boundary: String, imageData: Data, fileName: String, imageType: ImageType) -> Data {

    let body = NSMutableData()

    var mimetype = "image/png" // default
    if imageType == .jpeg {
        mimetype = "image/jpeg"
    }

    body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
    body.append("Content-Disposition:form-data; name=\"file\"; filename=\"\(fileName)\"\r\n".data(using: String.Encoding.utf8)!)
    body.append("Content-Type: \(mimetype)\r\n\r\n".data(using: String.Encoding.utf8)!)
    body.append(imageData)
    body.append("\r\n".data(using: String.Encoding.utf8)!)

    body.append("--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)

    return body as Data
}

@discardableResult func uploadImage(_ imageData: Data, fileName: String, imageType: ImageType, onSuccess: @escaping () -> Void, onFailure: @escaping (String) -> Void) -> Request {

    let boundary = generateBoundaryString()

    let request = service.resource("/files/upload").request(.post) {
        // See comments in Siesta Resource.swift class for .post
        [=10=].httpBody = self.constructHttpBodyForImageUpload(withBoundary: boundary, imageData: imageData, fileName: fileName, imageType: imageType)
        [=10=].addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
    }

    // My Server returns me JSON back
    request.onSuccess { entity in
        guard let json: [String: String] = entity.typedContent() else {
            onFailure("JSON parsing error")
            return
        }
        guard let status = json["status"] else {
            onFailure("Responce status is missing")
            return
        }
        print("status = \(status)")
    }.onFailure { (error) in
        onFailure(error.userMessage)
    }

    return request
}

} // YourAPI

要使用它(来自你的ViewController):

@IBAction func uploadImage(_ sender: AnyObject) {

    guard let image = self.imageView.image else {
        return
    }

    guard let imageData = UIImagePNGRepresentation(image) else {
        return
    }

    var fileName = self.fileName.text!
    if fileName.isEmpty {
        fileName = UUID().uuidString + ".png"
    }

    print("fileName = \(fileName)")

    YourAPI.sharedInstance.uploadImage(imageData, fileName: fileName, imageType: .png, onSuccess: {
        print("success")
    }, onFailure: { error in
        print(error)
    })
}

此外,最近几天在 Siesta 项目 guthub 上有一个拉取请求来实现:Convenience method for multipart/form-data requests,它很快就会成为框架的标准部分,并且可以减少上述样板代码的大小。