swift – 设置Alamofire自定义目标文件名,而不是使用suggestDown
发布时间:2020-12-14 02:25:27 所属栏目:百科 来源:网络整理
导读:我在桌面视图中获得了许多发票文件列表以及每个单元格中的许多下载按钮.当我单击其中一个时,它将下载发票文件.但问题是服务器响应建议文件名是“发票”. pdf“在我下载的每个文件中.因此,我需要手动编辑文件名,然后在下载后保存到文档.所以,如何在成功下载后
我在桌面视图中获得了许多发票文件列表以及每个单元格中的许多下载按钮.当我单击其中一个时,它将下载发票文件.但问题是服务器响应建议文件名是“发票”. 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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |