我正在使用框架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
所以我可以上传它。
有什么想法吗?
//// UPDATE
这就是死亡的地方:
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")
}
}
答案 0 :(得分:3)
以下是示例
此代码在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
}
比你可以执行上传
编辑关于将字符串转换为NSURL的评论
let URL: NSURL = NSURL(string: stringofURL)! //replace stringofURL to sPath
更新了您的代码
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")
}
}