0
我正在使用此guide下載文件在swift 3.我試圖通過添加一個特定的循環來操縱它,以便能夠下載n個文件。它可以工作,但它只能完成24 kb。下載swift 3中的文件只能完成24 kb?使用URLSession
這裏是我的代碼:
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")
}
}
}
結果:
是什麼原因導致它停止@ 24 KB?是因爲循環?或標識符操作?
會話保留其中的任務。但是,假設我有限的Swift知識不會誤解這裏發生的事情,問題在於代碼沒有保留** session **。 – dgatwood
嗨!對不起,遲到的回覆,我只能恢復編碼。它似乎是一個錯誤頁面。我重新檢查了我的鏈接,它的guid鏈接部分似乎有問題。雖然它正在工作,我正在做urlsession嗎?我將所需的數據放入urlsession標識符中,用於製作文件路徑。 – iOSNewbie