in reviewing swiftydropbox tutorial in v2 dropbox api, shows how perform download:
// download file let destination : (nsurl, nshttpurlresponse) -> nsurl = { temporaryurl, response in let filemanager = nsfilemanager.defaultmanager() let directoryurl = filemanager.urlsfordirectory(.documentdirectory, indomains: .userdomainmask)[0] // generate unique name file in case we've seen before let uuid = nsuuid().uuidstring let pathcomponent = "\(uuid)-\(response.suggestedfilename!)" return directoryurl.urlbyappendingpathcomponent(pathcomponent) } client.files.download(path: "/myfile.db", destination: destination).response { response, error in if let (metadata, url) = response { print("*** download file ***") let data = nsdata(contentsofurl: url) print("downloaded file name: \(metadata.name)") print("downloaded file url: \(url)") print("downloaded file data: \(data)") } else { print(error!) } }
i'm unclear what's going on destination
part. why need generate random string filename?
when try specify own filename, download doesn't seem work:
let destination : (nsurl, nshttpurlresponse) -> nsurl = { temporaryurl, response in let directoryurl = nsfilemanager.defaultmanager().urlsfordirectory(.documentdirectory, indomains: .userdomainmask)[0] return directoryurl.urlbyappendingpathcomponent("myfile.db") }
i want download file dropbox named myfile.db
, want put in device's documents directory name myfile.db
, overwrite if it's there.
how can that?
when doesn't seem work, expect mean error this:
error domain=nscocoaerrordomain code=516 "“cfnetworkdownload_bpyhu1.tmp” couldn’t moved “documents” because item same name exists." userinfo={nssourcefilepatherrorkey=..., nsuserstringvariant=( move ), nsdestinationfilepath=..., nsunderlyingerror=0x7fda0a67cea0 {error domain=nsposixerrordomain code=17 "file exists"}}
swiftydropbox, virtue of using alamofire, doesn't let overwrite files using download
function.
specifically, swiftydropbox calls download
in alamofire, , alamofire calls nsfilemanager.moveitematurl
. documentation nsfilemanager.moveitematurl
says:
if item same name exists @ dsturl, method aborts move attempt , returns appropriate error.
so, seems it's being cautious, , making hard app accidentally overwrite (ad potentially lose) data. if know want overwrite particular file, you'll need explicitly, after dropbox api call. we'll consider feature request though.
update: swiftydropbox offers ability overwrite files directly of version 3.1.0, e.g., using download(path:rev:overwrite:destination:).
Comments
Post a Comment