我在桌面视图中获得了许多发票文件列表以及每个单元格中的许多下载按钮.当我单击其中一个时,它将下载发票文件.但问题是服务器响应建议文件名是“发票”. pdf“在我下载的每个文件中.因此,我需要手动编辑文件名,然后在下载后保存到文档.所以,如何在成功下载后手动编辑文件名,并将其作为临时文件保存在文档中使用Alamofire.Request.suggestedDownloadDestination.
这是我的下载功能.
func downloadInvoice(invoice: Invoice,completionHandler: (Double?,NSError?) -> Void) { guard isInvoiceDownloaded(invoice) == false else { completionHandler(1.0,nil) // already have it return } let params = [ "Accesstoken" : “xadijdiwjad12121”] // Can’t use the destination file anymore because my server only return one file name “invoice.pdf” no matter which file i gonna download // So I have to manually edit my file name which i saved after it was downloaded. let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory,domain: .UserDomainMask) // So I have to save file name like that ““2016_04_02_car_invoice_10021.pdf” [Date_car_invoice_timestamp(Long).pdf] // Please look comment on tableView code Alamofire.Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders?.updateValue("application/pdf",forKey: "Content-Type") Alamofire.download(.POST,invoice.url,parameters:params,destination: destination) .progress { bytesRead,totalBytesRead,totalBytesExpectedToRead in print(totalBytesRead) dispatch_async(dispatch_get_main_queue()) { let progress = Double(totalBytesRead) / Double(totalBytesExpectedToRead) completionHandler(progress,nil) } } .responseString { response in print(response.result.error) completionHandler(nil,response.result.error) } }
这是表视图,它将检查下载的文件以及何时单击,在打开的功能中显示.
override func tableView(tableView: UITableView,didSelectRowAtIndexPath indexPath: NSIndexPath) { if let invoice = dataController.invoices?[indexPath.row] { dataController.downloadInvoice(invoice) { progress,error in // Todo: handle error print(progress) print(error) if (progress < 1.0) { if let cell = self.tableView.cellForRowAtIndexPath(indexPath),invoiceCell = cell as? InvoiceCell,progressValue = progress { invoiceCell.progressBar.hidden = false invoiceCell.progressBar.progress = Float(progressValue) invoiceCell.setNeedsdisplay() } } if (progress == 1.0) { // Here where i gonna get the downloaded file name from my model. // invoice.filename = (Assume “2016_04_02_car_invoice_10021”) if let filename = invoice.filename{ let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true) let docs = paths[0] let pathURL = NSURL(fileURLWithPath: docs,isDirectory: true) let fileURL = NSURL(fileURLWithPath: filename,isDirectory: false,relativetoURL: pathURL) self.docController = UIDocumentInteractionController(URL: fileURL) self.docController?.delegate = self if let cell = self.tableView.cellForRowAtIndexPath(indexPath) { self.docController?.presentOptionsMenuFromrect(cell.frame,inView: self.tableView,animated: true) if let invoiceCell = cell as? InvoiceCell { invoiceCell.accessoryType = .checkmark invoiceCell.setNeedsdisplay() } } } } } } }
所以,我的问题很简单.我只是不想使用该代码
let destination = Alamofire.Request.suggestedDownloadDestination(directory: .DocumentDirectory,domain: .UserDomainMask)
因为它使用response.suggestedfilename.And我想在选定的表视图单元格数据上手动保存文件名.任何帮助?请不要介意我在我的问题中发布了一些代码,因为我希望每个人都清楚地看到它.
目的地是类型(NSURL,NSHTTPURLResponse) – > NSURL.所以你可以做这样的事情
Alamofire.download(.POST,destination: { (url,response) -> NSURL in let pathComponent = "yourfileName" let fileManager = NSFileManager.defaultManager() let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory,inDomains: .UserDomainMask)[0] let fileUrl = directoryURL.URLByAppendingPathComponent(pathComponent) return fileUrl }) .progress { bytesRead,totalBytesExpectedToRead in print(totalBytesRead) dispatch_async(dispatch_get_main_queue()) { let progress = Double(totalBytesRead) / Double(totalBytesExpectedToRead) completionHandler(progress,nil) } } .responseString { response in print(response.result.error) completionHandler(nil,response.result.error) } }
Swift 3.0
在swift 3.0中它是DownloadFileDestination
Alamofire.download(url,method: .get,to: { (url,response) -> (destinationURL: URL,options: DownloadRequest.DownloadOptions) in return (filePathURL,[.removePrevIoUsFile,.createIntermediateDirectories]) }) .downloadProgress(queue: utilityQueue) { progress in print("Download Progress: \(progress.fractionCompleted)") } .responseData { response in if let data = response.result.value { let image = UIImage(data: data) } }
更多去Alamofire
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。