最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】swift中HTTP的download下载文件

Swift crifan 2918浏览 0评论

之前用的是:

daltoniam/SwiftHTTP · GitHub

https://github.com/daltoniam/SwiftHTTP

但是里面好像不支持download

swift Http download

ios – Download a file with NSURLSession in Swift – Stack Overflow

ios – Download file in swift – Stack Overflow

thibaultCha/TCBlobDownloadSwift · GitHub

不过,后来暂时还是用了之前的SwiftHTTP的get去暂时用做文件下载:

                //begin to download file
                downloadFromUrl_async(self.fileItem.downloadUrl, downloadCompleteHandler: downloadFileCompleteHandler)
func downloadFromUrl_async(url:String, downloadCompleteHandler:((String, Bool, NSData?, String?) -> Void)) {
    print("downloadFromUrl_async url=\(url)")
   
    var downloadOk:Bool = false
    var opt:HTTP? = nil
    let params:Dictionary<String, AnyObject>? = nil
    let headers:Dictionary<String, String>? = nil
   
    do {
        opt = try HTTP.GET(
            url,
            parameters: params,
            headers: headers,
            requestSerializer: JSONParameterSerializer())
   
        opt!.start { response in
            if let err = response.error {
                print("url=\(url) with params=\(params) response error= \(err.localizedDescription)")
               
                downloadOk = false
                downloadCompleteHandler(url, downloadOk, nil, err.localizedDescription)
                return
            }
           
            //print("response.statusCode=\(response.statusCode)")
            //print("opt finished: \(response.description)")
           
            downloadOk = true
            downloadCompleteHandler(url, downloadOk, response.data, nil)
        }
    } catch let httpGetError {
        print("url=\(url) with params=\(params) got error= \(httpGetError)")
       
        downloadOk = false
        downloadCompleteHandler(url, downloadOk, nil, "\(httpGetError)")
    }
}

可以得到文件的NSData:

respData=Optional(<504b0304 14000400 08006262 be40a5ab f7fe5a01 00003304 00001300 00005b43 6f6e7465 6e745f54 79706573 5d2e786d 6cac934d 4ec33010 85f73d85 e52d4adc b2400825 ed829f25 74510e60 ec4963d5 b12d8f5b dab3b1e0

转载请注明:在路上 » 【已解决】swift中HTTP的download下载文件

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.206 seconds, using 22.06MB memory