蒸汽客户端关闭流

时间:2017-08-18 02:26:11

标签: client vapor

App通过界面上传到服务器,服务器然后将图片上传到存储空间。我知道我的方法不是最好的如果有好方法感谢分享

代码如下所示:

使用main.swift

let config = try Config()
try config.addProvider(PostgreSQLProvider.Provider.self)

let drop = try Droplet(config)

let fleCrl = FileController();
fleCrl.addRoutest(drop: drop);
drop.resource("files", fleCrl);

使用FileController.swift

final class FileController: ResourceRepresentable{

var drop:Droplet?;

public func addRoutest(drop: Droplet) -> Void {
    self.drop = drop;
    let d = drop.grouped("file");
    d.post("updateFile", handler: updateFile)
}

func updateFile(req:Request) throws -> ResponseRepresentable {

    let picName =  req.data["name"]?.string ?? String();
    let bytes:[UInt8] = (req.data["data"]?.bytes)!;
    let request = Request(method:.post,uri:"http://up.qiniu.com");

    let t = "image/\(picName)";
    let token = req.data["token"]?.bytes;

    request.formData = [
        "token":Field(name:"token",filename:nil,part:Part(headers:[:], body: token!)),
        "file":Field(name:"file",filename:t,part:Part(headers:["Content-Type": "vnd.apple.mpegURL"],body:bytes)),
        "key":Field(name:"key",filename:t,part:Part(headers:[:],body:t.bytes))
    ]

    let resp = try self.drop?.client.respond(to: request);

    return resp?.data["info"]?.string;
   }

}

与客户

    let data = try Data.init(contentsOf: URL.init(string: "video.m3u8")!);

    Alamofire.upload(multipartFormData: { (multipartFormData) in

        multipartFormData.append(data!, withName: "data", fileName: "video", mimeType:"application/x-mpegURL")

        multipartFormData.append("videoName".data(using: .utf8)!, withName: "name")

    }, to: "http://0.0.0.0:8083/file/updateFile") { (encodingResult) in

        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .failure(let encodingError):
            print(encodingError)
        }
    }

服务器控制台报告如下

[Stream Error: The stream is closed] [Identifier: Transport.StreamError.closed]

0 个答案:

没有答案