0

I am using QuickLook to show remote files(server side files). but with this i am not getting total image or file its just showing extension in QLPreviewController why?

i have tried using this [enter link description here][1]

code: here urlAttach will look like this https://UUUUU.com/storage/bids/1631792917-9297.jpeg but showing in o/p only 1631792917-9297 why?

  import QuickLook

  class ViewVC: UIViewController, CustomCellDelegate {

    let previewController = QLPreviewController()
    var previewItems: [PreviewItem] = []

   func numberOfPreviewItems(in controller: QLPreviewController) -> Int { previewItems.count }
func quickLook(url: URL) {
        URLSession.shared.dataTask(with: url) { data, response, error in
            guard let data = data, error == nil else {
                //  in case of failure to download your data you need to present alert to the user
              //  self.presentAlertController(with: error?.localizedDescription ?? "Failed to download the pdf!!!")
                return
            }
            // you neeed to check if the downloaded data is a valid pdf
            guard
                let httpURLResponse = response as? HTTPURLResponse,
                let mimeType = httpURLResponse.mimeType,
                mimeType.hasSuffix("pdf")
            else {
                print((response as? HTTPURLResponse)?.mimeType ?? "")
              //  self.presentAlertController(with: "the data downloaded it is not a valid pdf file")
                return
            }
            do {
                // rename the temporary file or save it to the document or library directory if you want to keep the file
                let suggestedFilename = httpURLResponse.suggestedFilename ?? "quicklook.pdf"
                var previewURL = FileManager.default.temporaryDirectory.appendingPathComponent(suggestedFilename)
                try data.write(to: previewURL, options: .atomic)   // atomic option overwrites it if needed
              //  previewURL.hasHiddenExtension = true
                let previewItem = PreviewItem()
                previewItem.previewItemURL = previewURL
                self.previewItems.append(previewItem)
                DispatchQueue.main.async {
                    UIApplication.shared.isNetworkActivityIndicatorVisible = false
                    self.previewController.delegate = self
                    self.previewController.dataSource = self
                    self.previewController.currentPreviewItemIndex = 0
                    self.present(self.previewController, animated: true)
                 }
            } catch {
                print(error)
                return
            }
        }.resume()
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }

func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem { previewItems[index] }
 
func sharePressed(cell: ViewProposalTableVIewCell1) {
       guard let index = tableView.indexPath(for: cell)?.row else { return }

    let img = getBitDetails?.result?.bid?.get_attachments?[index].filename //bidAttatchment?[indexPath.item].filename

    let urlAttach = "https://farfromadate.com/storage/app/public/bids/" + img!

    let url = URL(string: urlAttach)
    
    quickLook(url: url!)
  
 }
Swift
  • 1,074
  • 12
  • 46
  • Why did you delete your last question? Btw your code looks familiar :) https://stackoverflow.com/a/28290719/2303865 – Leo Dabus Sep 17 '21 at 03:53
  • you are not showing your actual code. In your post you are moving the downloaded file to your documents directory and the filename is filename.pdf. Your screen shot shows 1631792917-9297 – Leo Dabus Sep 17 '21 at 04:06
  • @LeoDabus I have updated my code with you answer.. but here `QLPreviewController` is not opening.. please guide me where am i wrong – Swift Sep 17 '21 at 04:08
  • 1
    Use the correct path extension `jpeg` or `pdf`? Don't forget to upvote the answers that you find helpful – Leo Dabus Sep 17 '21 at 04:08
  • @LeoDabus, thank you `mimeType.hasSuffix("jpeg")` if i give like this its showing but.. if i want all kind of files(means .pdf, .jpeg, .png) then how to give filetype? – Swift Sep 17 '21 at 04:13
  • Just remove the condition or add multiple conditions `(mimeType.hasSuffix("jpeg") || mimeType.hasSuffix("pdf") || mimeType.hasSuffix("png"))` – Leo Dabus Sep 17 '21 at 04:17
  • suggestedFilename should give you the suggested name with the proper extension (mimeType) – Leo Dabus Sep 17 '21 at 04:19
  • @LeoDabus how to give .doc files are not opening if i add like this `mimeType.hasSuffix("png") || mimeType.hasSuffix("doc")` – Swift Sep 17 '21 at 10:33
  • @LeoDabus, how to show .doc files.. please do help – Swift Sep 17 '21 at 10:33
  • @LeoDabus, plz let me know how to show all files ... which condition should i remove – Swift Sep 17 '21 at 11:41
  • @LeoDabus except `jpeg` if i use any other file getting error `Error Domain=NSCocoaErrorDomain Code=260 "The file “1631882730-6964.pdf” couldn’t be opened because there is no such file."` – Swift Sep 17 '21 at 13:15
  • @LeoDabus, plz say something – Swift Sep 17 '21 at 13:15

0 Answers0