如何通过foldername将文件保存到文件夹中?

时间:2017-08-11 03:05:15

标签: swift3

我正在使用https://stackoverflow.com/a/32322851/7789222中的代码进行下载系统。这是一个伟大而完整的代码,但我可以找到一种方法将视图控制器中的foldername传递到下载文件到特定文件夹。任何人都可以帮我。我正在使用swift 3 xcode 8。

如果我在func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL)中对自定义目录进行硬编码,则每个文件都将下载到同一文件夹中。我想从视图控制器传递foldername,以便我可以将文件下载到不同的文件夹。我不能硬编码,因为我从服务器

检索文件名和foldername

1 个答案:

答案 0 :(得分:0)

示例中的目标网址由

提供
<ListView x:Name="list"
          Margin="0,0,0,0" 
          SelectionChanged="list_SelectionChanged" 
          VerticalAlignment="Top"  
          SelectionMode="Single" 
          ScrollViewer.HorizontalScrollBarVisibility="Visible"
          ScrollViewer.HorizontalScrollMode="Enabled">
</ListView>

(第17行)

您只需将目标文件夹网址传递给let destinationURL = try manager.url( for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false ).appendingPathComponent(url.lastPathComponent) 的初始值设定项即可替换示例中的目标网址:

DownloadOperation

您修改后的DownloadOperation看起来像这样:

let destinationURL = yourDestinationFolder.appendingPathComponent(url.lastPathComponent)

然后

添加操作的代码
class DownloadOperation : AsynchronousOperation {
    var task: URLSessionTask!
    let destinationFolder: URL

    init(session: URLSession, url: URL, destinationFolder: URL) {
        super.init()
        self.destinationFolder = destinationFolder
        task = session.downloadTask(with: url) { temporaryURL, response, error in
            defer { self.completeOperation() }

            guard error == nil && temporaryURL != nil else {
                print("\(error)")
                return
            }

            do {
                let manager = FileManager.default
                let destinationURL = destinationFolder.appendingPathComponent(url.lastPathComponent)
                _ = try? manager.removeItem(at: destinationURL)                    // remove the old one, if any
                try manager.moveItem(at: temporaryURL!, to: destinationURL)    // move new one there
            } catch let moveError {
                print("\(moveError)")
            }
        }
    }

    ...

}

如果您想使用DownloadManager:

queue.addOperation(DownloadOperation(session: session, url: url, destinationFolder: destinationFolder))

扩展名:

class DownloadManager {
    @discardableResult
    func addDownload(_ url: URL, to destinationFolder: URL) -> DownloadOperation {
        let operation = DownloadOperation(session: session, url: url, destinationFolder: destinationFolder)
        operations[operation.task.taskIdentifier] = operation
        queue.addOperation(operation)
        return operation
    }
    ...
}

然后您可以使用

添加下载
extension DownloadOperation: URLSessionDownloadDelegate {

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        do {
            let manager = FileManager.default
            let destinationURL = destinationFolder.appendingPathComponent(downloadTask.originalRequest!.url!.lastPathComponent)
            if manager.fileExists(atPath: destinationURL.path) {
                try manager.removeItem(at: destinationURL)
            }
            try manager.moveItem(at: location, to: destinationURL)
        } catch {
            print("\(error)")
        }
    }

    ...
}