How to display a base64 file

Hi guys


I have a java rest service answering with a base64 file content (for example a pdf or an image). But the first question is: which is the best way to display the file content?

Unsure on what to do, I decided to put a webkit view in my page and to execute the following code:


if let decodeData: Data = Data(base64Encoded: download.fileContent!, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters) {
     DispatchQueue.main.sync() {
          self.webView.load(decodeData, mimeType: "application/pdf", characterEncodingName: "utf-8", baseURL: URL(fileURLWithPath: ""))
     }
}


But the file isn't displayed in the webview and the console logs the error: "failed to find PDF header: `%PDF' not found"


I don't know how to solve the problem. I'm using swift4 and xcode9.


Thanks in advance

It’s pretty clear that the data you got back from the Base64 decode is not a PDF. PDF files all start with the sequence 25 50 44 46, that is, the ASCII bytes for

%PDF
. I recommend that you dump the data to see what you’re actually getting.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I receive a base64 string pdf from a rest service. It is a binary file content encoded in java with the method "Base64.getEncoder().encodeToString(fileContent)".


In javascript i convert it into a binary array and i save it to the disk. In swift4 i tried the previuous method but it seems do not work. Any idea?

His idea was to dump out the data to see what you're actually getting, not what you think you're getting.

How do i dump data in xcode9?

I printed the data in base64 string and the result is exactly what i obtain on a converter in internet. But the pdf is not displayed in webkit

I think at this point you have two options:


1. File a bug report containing the PDF file and your code, then submit a developer tech support incident (described here: https://developer.apple.com/support/technical/

).


2. Post your question somewhere like Stack Overflow where you can post more of your code, any relevant links, and more information.

How do i dump data in xcode9?

You can do this using

NSLog
:
let d: Data = …
NSLog("%@", d as NSData)

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I've finally found the solution. I post it there with the hope it can be usefull to someone.


I've simply decoded the base64 string twice:


let fromDataToString = String(data: download.fileContent!, encoding: .isoLatin1)
let fromStringToData = Data(base64Encoded: fromDataToString!, options: .ignoreUnknownCharacters)


Then i was able to display the file in the webkit view.


Thanks to all

For the record, you aren't decoding twice. In your first post, you used a method that assumed UTF8 string data. Now you are specifying Latin1 and explicitly creating a string. I would expect either encoding to represent base 64 data the same way. There is something funky about your input data.

Based on what I read java uses latin1 for enc/decoding base64 string and probably mongodb driver does the same

Base64 is ASCII compatible, so you’ll get the same representation in any 8-bit encoding that starts with ASCII (and that includes both ISO Latin-1 and UTF-8). If I were in your shoes I’d spend the time getting to the bottom of this problem rather than relying on this double decode workaround.

To investigate this further you could:

  1. Decode the data both ways

  2. Write that data out to a temporary file

  3. Dump the files to see what’s different

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

But why? You only need base64 if your transport layer is text-based, like XML or JSON. Perhaps the problem is how you are extracting data out of that transport layer. For whatever reason, your explicit Latin1 decoding is apparently doing a better job of that. That alone should be cause for concern since any decent text-based transport layer should be using UTF-8. I strongly suggest getting to the bottom of this issue now, before you ship this to people who use really funky text encodings.

Yeah, my solution is quite a workaround. I have to finish a work within 4 days and I'll spend a lot of time to investigate this problem. #staytuned


Thanks for all

How to display a base64 file
 
 
Q