swift - 尝试在 Swift 中上传时无法读取本地文件

标签 swift sftp

我正在使用 DLFTPClient 进行 sftp 上传。这是我的代码。

var connection: DLSFTPConnection = DLSFTPConnection(hostname: "192.168.1.1", port: 2222, username: "test", password: "test")
var remoteBasePath = "/test"
var localPath: String = Bundle.main.url(forResource: "test", withExtension: ".jpg")?.absoluteString
var request: DLSFTPRequest?


override func viewDidLoad() {
    super.viewDidLoad()

    var successBlock = {() -> Void in
        DispatchQueue.main.async(execute: {() -> Void in
            // login successful
            print("loginSuccess")
        })
        } as? DLSFTPClientSuccessBlock

    var failureBlock = {(_ error: Error?) -> Void in
        DispatchQueue.main.async(execute: {() -> Void in

            print(error)
        })
        print("error")
        } as? DLSFTPClientFailureBlock
    connection.connect(successBlock: successBlock, failureBlock: failureBlock)
}


@IBAction func startTapped(_ sender: Any) {
    print("sending")

    var successBlock = {(_ file: DLSFTPFile, _ startTime: Date, _ finishTime: Date) -> Void in
        DispatchQueue.main.async(execute: {() -> Void in

            var alertView = UIAlertView(title: "Upload completed", message: "", delegate: nil, cancelButtonTitle: "OK", otherButtonTitles: "")
            alertView.show()
        })
        print("success")
        } as? DLSFTPClientFileTransferSuccessBlock

    var failureBlock = {(_ error: Error?) -> Void in
        DispatchQueue.main.async(execute: {() -> Void in

            print(error)
        })
        print("error")
        } as? DLSFTPClientFailureBlock

    var localFilename = "test.jpg"
    var remotePath: String = URL(fileURLWithPath: remoteBasePath).appendingPathComponent(localFilename).absoluteString
    request = DLSFTPUploadRequest(remotePath: remotePath, localPath: localPath, successBlock: successBlock, failureBlock: failureBlock, progressBlock: nil)
    connection.submitRequest(request)

}

当我尝试登录时,一切似乎都正常,它返回成功。但是,当我尝试上传文件时,它返回错误:(Error Domain=SFTPClientErrorDomain Code=29“本地文件不可读”UserInfo={NSLocalizedDescription=本地文件不可读})

我的远程基本路径和本地路径有问题吗?提前非常感谢您。

最佳答案

我在 localPath 和 remotePath 中使用 .path 而不是 .absoluteString 修复了它。

关于swift - 尝试在 Swift 中上传时无法读取本地文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46968011/

相关文章:

json - 嵌套 JSON Feed 返回错误

swift - 将 UICollectionView 集中在 tvOS 上的行

java - 使用 JSCH 在远程服务器上获取 MD5 校验和

linux - BASH - 传递给 sFTP 的带空格的文件名无法正确解析

python - 使用 Paramiko Python 模块时如何避免这些 keepalive@openssh.com 日志消息?

ios - 应用程序在 textview.becomeFirstResponder 处崩溃

swift - AVPlayer无法播放视频

ios - 当动画为假时,如何检测 UIScrollView 已完成滚动?

java - 使用带有公钥/私钥身份验证的 JSch 从远程 SFTP 服务器获取数据。有什么例子吗?

java - SFTP 连接器 DH key 错误