gpt4 book ai didi

swift - 使用 Swift 上传文件到 FTP 服务器

转载 作者:搜寻专家 更新时间:2023-11-01 06:41:48 31 4
gpt4 key购买 nike

我正在使用框架 RebekkaTouch 将文件从我的 Swift 应用程序上传到 FTP 服务器,就像:

if let URL = NSBundle.mainBundle().URLForResource("myFile", withExtension: "txt") {
let path = "myFile.txt"
session.upload(URL, path: path) {
(result, error) -> Void in
print("result:\n\(result), error: \(error)\n\n")
}
}

这非常适合我通过 Xcode 手动上传的文件。但是我似乎无法找到我下载并动态存储在本地 Documents 目录中的文件的文件路径。

说,我知道我有这个文件:/private/var/mobile/Containers/Data/Application/3D92EA55-01E0/Documents/SomeFile.txt

我知道它在那里,因为我在遍历 NSFileManager.defaultManager() 后获得了路径,但我不能转换它没有 NSURL 所以我可以上传它。

有什么想法吗?

////更新

这是死亡的地方:

let file = "myFile.txt"
if let dir : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
//path will be stored here
let sPath = dir.stringByAppendingPathComponent(file);
print(sPath) // printing the file path

let url: NSURL = NSURL(string: sPath)!

let destinationFile = "myFile.txt"
FTPSession.upload(url, path: destinationFile) { // <-- Dies here
(result, error) -> Void in
print("- Result:\n\(result), error: \(error)\n\n")
}
}

最佳答案

这是例子

This code is fully tested on Swift 2.0

 let file = "SomeFile.txt"
if let dir : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
//path will be stored here
let sPath = dir.stringByAppendingPathComponent(file);

print(sPath) // printing the file path
}

比你可以执行上传

EDIT AS ASKED IN COMMENT ABOUT CONVERTING STRING TO NSURL

let URL: NSURL = NSURL(string: stringofURL)! //replace stringofURL to sPath

UPDATED YOUR CODE

let file = "myFile.txt"
if let dir : NSString = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainsMask, true).first {
//path will be stored here
let sPath = dir.stringByAppendingPathComponent(file);
print(sPath) // printing the file path

let url: NSURL = NSURL(string: sPath)!

let destinationFile = "myFile.txt"
session.upload(url, path: destinationFile) { // here was the error it should be session not FTPsession
(result, error) -> Void in
print("- Result:\n\(result), error: \(error)\n\n")
}
}

关于swift - 使用 Swift 上传文件到 FTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34335867/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com