How to download the file from Parse.com using the

2019-06-24 15:17发布

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.

I tried to access the URL only, but it returns an error.

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?

This is the error I get:

Error Domain=NSURLErrorDomain Code=-1100 "The requested URL was not found on this server." UserInfo={NSUnderlyingError=0x7fa39940dcc0 {Error Domain=kCFErrorDomainCFNetwork Code=-1100 "(null)"}

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()
}

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";

1条回答
Juvenile、少年°
2楼-- · 2019-06-24 15:58

I bet your problem is in this line:

let url = NSURL(fileURLWithPath: str)

"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.

Why not do:

let url = NSURL(string: str)

and see if that works better?

查看更多
登录 后发表回答