问题描述
我在 Parse.com文件中找到了这个问题下载问题,但是仅提及可以从网址"下载文件.另外, Parse.com REST文档仅讨论上传文件并与目的.
I found this question at Parse.com file download question, however, it only mentions that the file can be downloaded from the Url. Also, the Parse.com REST Documentation only discuss upload file and associated with an Object.
我尝试仅访问URL,但返回错误.
I tried to access the URL only, but it returns an error.
任何人都可以在Swift中使用REST API进行示例,并在查询对象后获得URL后如何下载实际文件?
Can anyone help with an example in Swift and using the REST API, how to download the actual file once you get the URL after querying the object?
这是我得到的错误:
Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo={NSUnderlyingError=0x7fa39940dcc0 {Error Domain=kCFErrorDomainCFNetwork Code=-1100 "(null)"}
这是我在Swift 2.0中的代码:
This is my code in Swift 2.0:
func downloadFile(){
let str = "http://files.parsetfss.com/c426b506-44da-447d-91d0-93f13980758b/tfss-127e50c4-be6e-4228-b1a3-3f253358ac24-pic.jpg"
let request = NSMutableURLRequest()
request.HTTPMethod = "GET"
request.addValue(appID, forHTTPHeaderField: "X-Parse-Application-Id")
request.addValue(apiKey, forHTTPHeaderField: "X-Parse-REST-API-Key")
let url = NSURL(fileURLWithPath: str)
request.URL = url
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithRequest(request, completionHandler: {
(data, response, error) in
if (error == nil) {
do {
let image = try NSJSONSerialization.dataWithJSONObject(data!, options: [])
} catch {
}
}
})
task.resume()
}
这是查询对象时得到的JSON响应,而url是获取文件所需的URL:
This is the JSON response I get when I query the object, and the url is what needs to be used in order to get the file:
"picture": {
"__type" = File;
name = "tfss-127e50c4-be6e-4228-b1a3-3f253358ac24-pic.jpg";
url = "http://files.parsetfss.com/c426b506-44da-447d-91d0-93f13980758b/tfss-127e50c4-be6e-4228-b1a3-3f253358ac24-pic.jpg";
推荐答案
我敢打赌您的问题在这一行:
I bet your problem is in this line:
let url = NSURL(fileURLWithPath: str)
"str
"是一个远程URL,而不是本地文件路径,该API尝试执行的操作是从您提供的字符串中创建一个本地"file:///
" URL.
"str
" is a remote URL and not a local file path, and what that API is trying to do is create a local "file:///
" url from the string you provided it.
为什么不这么做:
let url = NSURL(string: str)
看看效果更好吗?
这篇关于如何使用REST API从Parse.com下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!