下载swift3中的文件最多只能完成24kb?使用 URLSession
Downloading files in swift 3 only completes up to 24 kb? Using URLSession
我正在使用这个 guide 下载 swift 中的文件 3. 我试图通过添加某个循环来操纵它,以便能够下载 n 个文件。它有效,但最多只能完成 24 kb。
这是我的代码:
for jsonDict in data{
var jasonData = Attachment()
if let data = (jsonDict["url"] as? String) {jasonData.url = data} else { jasonData.url = ""}
self.beginDownloadFile(jasonData: jasonData)
}
func beginDownloadFile(jasonData: Attachment){
let identifier = jasonData.customer_id + "/attachment/" + jasonData.patient_guid + "@%E%@" + "/" + jasonData.filename + "@%E%@" + jasonData.customer_id + "@%E%@" + jasonData.patient_guid
let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: identifier)
backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)
var link = self.constants.PATIENTS_PICTURE_LINK + jasonData.customer_id + "/attachment/" + jasonData.guid + "/" + jasonData.filename
link = link.replacingOccurrences(of: " ", with: "%20")
let url = URL(string: link)!
downloadTask = backgroundSession.downloadTask(with: url)
downloadTask.resume()
}
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL){
let string_container = session.configuration.identifier!.components(separatedBy: "@%E%@")
let folderPath = string_container[0]
let fileName = string_container[1]
let customerID = string_container[2]
let patientGUID = string_container[3]
print("GUID IS: " + patientGUID)
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentDirectoryPath:String = path[0]
let fileManager = FileManager()
let path1 = documentDirectoryPath.stringByAppendingPathComponent(pathComponent: customerID)
let path2 = path1.stringByAppendingPathComponent(pathComponent: "attachment")
let path3 = path2.stringByAppendingPathComponent(pathComponent: patientGUID)
let destinationURLForFile = URL(fileURLWithPath: path3.appendingFormat(fileName))
do {
if !fileManager.fileExists(atPath: path1) {
try fileManager.createDirectory(atPath: path1, withIntermediateDirectories: false, attributes: nil)
}
if !fileManager.fileExists(atPath: path2) {
try fileManager.createDirectory(atPath: path2, withIntermediateDirectories: false, attributes: nil)
}
if !fileManager.fileExists(atPath: path3) {
try fileManager.createDirectory(atPath: path3, withIntermediateDirectories: false, attributes: nil)
}
} catch let error as NSError {
print("ATTACHMENT ERROR: " + error.localizedDescription);
}
if fileManager.fileExists(atPath: destinationURLForFile.path){
//showFileWithPath(path: destinationURLForFile.path)
do {
try fileManager.removeItem(at: destinationURLForFile)
try fileManager.moveItem(at: location, to: destinationURLForFile)
//showFileWithPath(path: destinationURLForFile.path)
}catch{
print("An error occurred while moving/deleting file to destination url")
}
}
else{
do {
try fileManager.moveItem(at: location, to: destinationURLForFile)
//showFileWithPath(path: destinationURLForFile.path)
}catch{
print("An error occurred while moving file to destination url")
}
}
}
结果:
Sample Screenshot
是什么原因导致它在 24 kb 时停止?是因为循环吗?还是标识符操纵?
您声明 downloadTask
的代码在哪里?是实例吗属性?
如果是这样,那么每次循环运行时您都会丢弃指向 downloadTask
的指针,并且此代码得到 运行:
downloadTask = backgroundSession.downloadTask(with: url)
downloadTask.resume()
您的任务可能因此提前发布。如果是这种情况,那么下载应该完成循环的 last 时间。
另外,24k 是否是正确的文件大小?或者静态错误页面的大小?
我正在使用这个 guide 下载 swift 中的文件 3. 我试图通过添加某个循环来操纵它,以便能够下载 n 个文件。它有效,但最多只能完成 24 kb。
这是我的代码:
for jsonDict in data{
var jasonData = Attachment()
if let data = (jsonDict["url"] as? String) {jasonData.url = data} else { jasonData.url = ""}
self.beginDownloadFile(jasonData: jasonData)
}
func beginDownloadFile(jasonData: Attachment){
let identifier = jasonData.customer_id + "/attachment/" + jasonData.patient_guid + "@%E%@" + "/" + jasonData.filename + "@%E%@" + jasonData.customer_id + "@%E%@" + jasonData.patient_guid
let backgroundSessionConfiguration = URLSessionConfiguration.background(withIdentifier: identifier)
backgroundSession = Foundation.URLSession(configuration: backgroundSessionConfiguration, delegate: self, delegateQueue: OperationQueue.main)
var link = self.constants.PATIENTS_PICTURE_LINK + jasonData.customer_id + "/attachment/" + jasonData.guid + "/" + jasonData.filename
link = link.replacingOccurrences(of: " ", with: "%20")
let url = URL(string: link)!
downloadTask = backgroundSession.downloadTask(with: url)
downloadTask.resume()
}
func urlSession(_ session: URLSession,
downloadTask: URLSessionDownloadTask,
didFinishDownloadingTo location: URL){
let string_container = session.configuration.identifier!.components(separatedBy: "@%E%@")
let folderPath = string_container[0]
let fileName = string_container[1]
let customerID = string_container[2]
let patientGUID = string_container[3]
print("GUID IS: " + patientGUID)
let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
let documentDirectoryPath:String = path[0]
let fileManager = FileManager()
let path1 = documentDirectoryPath.stringByAppendingPathComponent(pathComponent: customerID)
let path2 = path1.stringByAppendingPathComponent(pathComponent: "attachment")
let path3 = path2.stringByAppendingPathComponent(pathComponent: patientGUID)
let destinationURLForFile = URL(fileURLWithPath: path3.appendingFormat(fileName))
do {
if !fileManager.fileExists(atPath: path1) {
try fileManager.createDirectory(atPath: path1, withIntermediateDirectories: false, attributes: nil)
}
if !fileManager.fileExists(atPath: path2) {
try fileManager.createDirectory(atPath: path2, withIntermediateDirectories: false, attributes: nil)
}
if !fileManager.fileExists(atPath: path3) {
try fileManager.createDirectory(atPath: path3, withIntermediateDirectories: false, attributes: nil)
}
} catch let error as NSError {
print("ATTACHMENT ERROR: " + error.localizedDescription);
}
if fileManager.fileExists(atPath: destinationURLForFile.path){
//showFileWithPath(path: destinationURLForFile.path)
do {
try fileManager.removeItem(at: destinationURLForFile)
try fileManager.moveItem(at: location, to: destinationURLForFile)
//showFileWithPath(path: destinationURLForFile.path)
}catch{
print("An error occurred while moving/deleting file to destination url")
}
}
else{
do {
try fileManager.moveItem(at: location, to: destinationURLForFile)
//showFileWithPath(path: destinationURLForFile.path)
}catch{
print("An error occurred while moving file to destination url")
}
}
}
结果:
Sample Screenshot
是什么原因导致它在 24 kb 时停止?是因为循环吗?还是标识符操纵?
您声明 downloadTask
的代码在哪里?是实例吗属性?
如果是这样,那么每次循环运行时您都会丢弃指向 downloadTask
的指针,并且此代码得到 运行:
downloadTask = backgroundSession.downloadTask(with: url)
downloadTask.resume()
您的任务可能因此提前发布。如果是这种情况,那么下载应该完成循环的 last 时间。
另外,24k 是否是正确的文件大小?或者静态错误页面的大小?