文件大小不在视频大小的标题中,以计算进度

时间:2018-10-04 21:35:17

标签: swift http https nsurlsession

从马特的评论中,我看到我不得不改变这个问题。谢谢马特。  下载图像时,我得到了totalBytesExpectedToWrite,但是下载视频时,我得到了-1.0,我已经使用在线标头测试器检查了标头,并且标头具有预期的总字节数,并且还运行了一些Swift代码来检查响应。如何获得totalBytesExpectedToWrite以便创建进度条?

func begienDownloadingFile() {
    if !self.internetViderPathVar.isEmpty{ 
        let configuration = URLSessionConfiguration.default
        let operationQueue = OperationQueue()
        let urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: operationQueue)

        guard let url = URL(string: self.self.testURL) else { return }

        let downloadTask = urlSession.downloadTask(with: url)
        downloadTask.resume()

    }else{
        //Do nothing
    }
}

func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
    print("Total bytes written -> ", totalBytesWritten)
    print("Total bytes expected -> ", totalBytesExpectedToWrite)

    let precentage = CGFloat(totalBytesWritten) / CGFloat(totalBytesExpectedToWrite)
}

这是用于获取HEAD进行测试的代码。

func getDownloadSize(url: URL, completion: @escaping (Int64, Error?) -> Void) {
    let timeoutInterval = 5.0
    var request = URLRequest(url: url,
                             cachePolicy: .reloadIgnoringLocalAndRemoteCacheData,
                             timeoutInterval: timeoutInterval)
    request.httpMethod = "HEAD"
    URLSession.shared.dataTask(with: request) { (data, response, error) in
        print("The response -> ",response as Any)
        let contentLength = response?.expectedContentLength ?? NSURLSessionTransferSizeUnknown
        completion(contentLength, error)
        }.resume()
}


func testSize() {
    let url = URL(string: testURL3)!
    getDownloadSize(url: url, completion: { (size, error) in
        if error != nil {
            print("An error occurred when retrieving the download size: \(String(describing: error?.localizedDescription))")
        } else {
            print("The download size is \(size).")
        }
    })
}

这是视频的响应。

The response ->  Optional(<NSHTTPURLResponse: 0x1d4236600> { URL: example.com/VBC/VBCVideo/EE9A0A33-2387-4DB3-B212-37C60835002F-VIDEO.mov } { Status Code: 200, Headers {
"Accept-Ranges" =     (
    bytes
);
Connection =     (
    "Keep-Alive"
);
"Content-Encoding" =     (
    gzip
);
"Content-Type" =     (
    "video/quicktime"
);
Date =     (
    "Wed, 10 Oct 2018 17:08:53 GMT"
);
Etag =     (
    "\"ae000f3-1030f79-563b13f3de4f2-gzip\""
);
"Keep-Alive" =     (
    "timeout=5"
);
"Last-Modified" =     (
    "Fri, 26 Jan 2018 17:30:04 GMT"
);
Server =     (
    Apache
);
Vary =     (
    "Accept-Encoding,User-Agent"
);
} })
The download size is -1.

这是jpeg的响应。

The response ->  Optional(<NSHTTPURLResponse: 0x1d023c5e0> { URL: example.com/VBC/VBCContact/58120B41-61E1-4297-9448-3306BA68B799-CONTACT.jpg } { Status Code: 200, Headers {
"Accept-Ranges" =     (
    bytes
);
Connection =     (
    "Keep-Alive"
);
"Content-Length" =     (
    120409
);
"Content-Type" =     (
    "image/jpeg"
);
Date =     (
    "Wed, 10 Oct 2018 17:06:55 GMT"
);
Etag =     (
    "\"ae02d37-1d659-55c66e70375d0\""
);
"Keep-Alive" =     (
    "timeout=5"
);
"Last-Modified" =     (
    "Wed, 25 Oct 2017 22:51:55 GMT"
);
Server =     (
    Apache
);
} })
The download size is 120409.

以下是网站的回复 该工具允许您检查状态代码,响应头,重定向位置和HTTP连接的重定向链。所有响应HTTP标头的列表。

View HTTP Response Header

HTTP Status for: "example.com/VBC/VBCVideo/EE9A0A33-2387-4DB3-B212-37C60835002F-VIDEO.mov"

HTTP/1.1 200 OK
Date: Wed, 10 Oct 2018 17:18:40 GMT
Server: Apache
Last-Modified: Fri, 26 Jan 2018 17:30:04 GMT
ETag: "ae000f3-1030f79-563b13f3de4f2"
Accept-Ranges: bytes
Content-Length: 16977785
Vary: Accept-Encoding,User-Agent
Connection: close
Content-Type: video/quicktime

0 个答案:

没有答案