- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
错误(FTP 下载)-6ren"> 错误(FTP 下载)-我的应用程序(Swift 5、Xcode 10、iOS 12)的第一个 View 有一个“用户名”TextField和“登录”Button 。单击该按钮会检查我的 FTP 服务器上是否存在与输入的用户-6ren">
我的应用程序(Swift 5、Xcode 10、iOS 12)的第一个 View 有一个“用户名”TextField
和“登录”Button
。单击该按钮会检查我的 FTP 服务器上是否存在与输入的用户名对应的文件,并将其下载到 Documents
设备上的文件夹。为此,我使用 FileProvider .
我的代码:
private func download() {
print("start download") //Only called once!
let foldername = "myfolder"
let filename = "mytestfile.txf"
let server = "192.0.0.1"
let username = "testuser"
let password = "testpw"
let credential = URLCredential(user: username, password: password, persistence: .permanent)
let ftpProvider = FTPFileProvider(baseURL: server, mode: FTPFileProvider.Mode.passive, credential: credential, cache: URLCache())
ftpProvider?.delegate = self as FileProviderDelegate
let fileManager = FileManager.default
let source = "/\(foldername)/\(filename)"
let dest = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent(filename)
let destPath = dest.path
if fileManager.fileExists(atPath: destPath) {
print("file already exists!")
do {
try fileManager.removeItem(atPath: destPath)
} catch {
print("error removing!") //TODO: Error
}
print("still exists: \(fileManager.fileExists(atPath: destPath))")
} else {
print("file doesn't already exist!")
}
let progress = ftpProvider?.copyItem(path: source, toLocalURL: dest, completionHandler: nil)
progressBar.observedProgress = progress
}
我正在检查设备上是否已存在该文件,因为 FileProvider
似乎没有提供 copyItem
下载功能还可以让您覆盖本地文件。
问题是 copyItem
尝试做两次所有事情:第一次下载文件成功(它实际上存在于 Documents
中,我检查过),因为我手动删除该文件(如果它已经存在)。第二次尝试失败,因为该文件已存在并且此 copyItem
函数不知道如何覆盖,当然也不会调用我的代码来再次删除原始代码。
我可以做什么来解决这个问题?
编辑/更新:
我在我的 ftp 服务器的根目录下创建了一个简单的“sample.txt”(里面的文本:“来自 example.txt 的 Hello world!”),然后尝试读取该文件以便稍后自己保存。为此,我使用“Sample-iOS.swift”文件 here 中的代码.
ftpProvider?.contents(path: source, completionHandler: {
contents, error in
if let contents = contents {
print(String(data: contents, encoding: .utf8))
}
})
但它也执行了两次! “sample.txt”文件的输出是:
Optional("Hello world from sample.txt!")
Fetching on sample.txt succeed.
Optional("Hello world from sample.txt!Hello world from sample.txt!")
Fetching on sample.txt succeed.
为什么它也调用两次?我只调用我的函数一次,并且“开始下载”也只打印一次。
编辑/更新2:
我做了更多调查,发现 contents
中被称为两次。功能:
self.ftpDownload
部分!self.ftpRetrieve
部分是打了两次电话。self.attributesOfItem
部分被调用两次。 ftpProvider?.copyItem
使用相同的 ftpDownload
func,所以至少我知道为什么两者 contents()
和copyItem()
受到影响。
但同样的问题仍然存在:为什么它会调用这些函数两次以及如何解决这个问题?
最佳答案
这不是一个显示 FileProvider 实际修复的答案!
不幸的是,该库目前有很多错误,函数被调用两次(您可以通过使用“firstTimeCalled” bool 检查来防止),并且如果服务器很慢(-ish),您也可能无法获得例如目录中文件的完整列表,因为 FileProvider 在服务器实际完成之前停止接收答案。
我还没有找到任何其他适用于 Swift 的 FTP 库(并且仍然受支持),所以现在我正在使用 BlueSocket (它能够打开套接字,向服务器发送命令并从中接收命令)并构建了我自己的小型库,可以发送/接收......围绕它的文件(使用 FTP 代码)。
关于ios - FileProvider: "CopyItem()"被调用两次 -> 错误(FTP 下载),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56113593/
我在 Windows 7 中使用下面的代码进行文件复制操作,但它不起作用,并且在运行或调试时没有任何错误,当应用程序调用 CoGetObject 时,它会崩溃并且无法运行返回任何 Hresult 错误
我的应用程序(Swift 5、Xcode 10、iOS 12)的第一个 View 有一个“用户名”TextField和“登录”Button 。单击该按钮会检查我的 FTP 服务器上是否存在与输入的用户
我正在 Hook 函数,即 IFileOperation::CopyItems 来实现文件复制监视。我的代码在 windows-7 32 位机器上运行完美,但在 windows-7 64 位机器上崩溃
我正在使用 swift ubuntu docker:https://github.com/IBM-Swift/swift-ubuntu-docker 然后尝试将文件从路径 A 复制到路径 B。在执行过
我是一名优秀的程序员,十分优秀!