如何使用REST API从Parse.com下载文件?

编程入门 行业动态 更新时间:2024-10-19 08:45:21
本文介绍了如何使用REST API从Parse下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 Parse文件中找到了这个问题下载问题,但是仅提及可以从网址"下载文件.另外, Parse REST文档仅讨论上传文件并与目的.

I found this question at Parse file download question, however, it only mentions that the file can be downloaded from the Url. Also, the Parse 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 = "files.parsetfss/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 = "files.parsetfss/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下载文件?

本文发布于:2023-11-28 10:01:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641948.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   文件   API   REST   Parse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!