【发布时间】:2015-08-17 21:40:54
【问题描述】:
我想上传最近从 DocumentPicker 下载的文件(在我的应用程序中)(例如 iCloud、Dropbox、...)
所以我有这个文件的 NSUrl (urlToFile)(已验证)
我用这个代码上传:
upload(.POST, Config.uploadURL, urlToFile!)
.progress { (bytesWritten, bytesWrittenInTotal, bytesToBeWritten) -> Void in
println("Bytes: \(bytesWrittenInTotal)")
}
.responseString{(request, response, JSON, error) in
println(request)
println(response)
println(JSON)}
根本没有上传连接。接收上传的 php 文件具有日志功能,因此每次调用文件都会被记录。上面显示的代码永远不会建立连接。
这是我收到的输出:
{ 网址:http://correctURLtoThePHPScript }
无
无
错误信息:
操作无法完成。是目录
我认为这是由于 documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) 返回带有斜杠的 fileUrl。但是,即使我通过下面的代码删除了斜杠,我也会收到相同的错误消息!
let s = url.URLString
let strippedUrl = s.substringToIndex(advance(s.startIndex, count(s)-1))
urlToFile = NSURL(string: strippedUrl)
解决方案:
使用urlToFile = NSURL(fileURLWithPath: strippedUrl)!!
【问题讨论】:
-
请打印出错误参数,以便我们调试此代码。另外:
Config.uploadURL是正确的,对吧? -
感谢您的快速答复。错误是:Error: Optional(Error Domain=NSPOSIXErrorDomain Code=21 "The operation could not be completed. Is a directory" UserInfo=0x7fd3b261b9d0 {_kCFStreamErrorCodeKey=21, _kCFStreamErrorDomainKey=1})跨度>
-
看起来文件的 URL 真的是目录的 URL?
-
好吧。 DocumentPicker 的返回值是
NSUrl,其中包含完整的 filePath 和 fileName,但带有一个结束 / 字符!我需要摆脱它,保持有效的 NSUrl -
哈哈!始终打印/使用 NSError 数据:这是 Alamofire 与您“对话”的唯一方式
标签: ios swift upload alamofire