3

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

谢谢你。

4

2 回答 2

3

其实上面的答案并不完全正确。由于 Siesta 长期以来一直支持 POST 请求,因此您可以通过以下方式在 YourAPI 类中进行文件上传(内容类型: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
        $0.httpBody = self.constructHttpBodyForImageUpload(withBoundary: boundary, imageData: imageData, fileName: fileName, imageType: imageType)
        $0.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 上有一个拉取请求要实施:Multipart/form-data requests 的便捷方法,它很快就会成为框架的标准部分,并且可以减少上述样板代码的大小。

于 2018-06-23T16:43:31.553 回答
1

根据Siesta 的 GitHub 页面目前不支持文件上传/下载任务。请看这张对比图:

对比图

于 2017-12-15T11:29:50.583 回答