# System.perspective.download Encoding

**URL:** <https://forum.inductiveautomation.com/t/system-perspective-download-encoding/38555>\
**Category:** Ignition\
**Created:** [September 10, 2020, 2:32pm UTC](https://forum.inductiveautomation.com/t/system-perspective-download-encoding/38555 "2020-09-10T14:32:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@victor1](https://forum.inductiveautomation.com/u/victor1)\
**Post date:** [September 10, 2020, 2:32pm UTC](https://forum.inductiveautomation.com/t/system-perspective-download-encoding/38555/1 "2020-09-10T14:32:39Z")

</div>

I’m trying to download a PDF, but when I open it everything is blank.  
If I open it with notepad and compare it with the original PDF, that’s almost right. I think the error is because of the encoding.

Original:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/7/3/736ce2a3931172e81c5b6e04713649d39cb020ee.png)

Downloaded:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/7/c7d1f1f613dae9a9e646ab32ac5ddc333acb492c.png)

In the bottom right corner of the notepad is always UTF-8 when I download it, how can I change this?

My code:

```auto
    access_token = 'ya29.a0Af...'
	
	uri = 'https://www.googleapis.com/drive/v3/files/1-fS4Bgy7NCdM8Frsed1YNOTaR_8majxm?alt=media'
	headers = {'Authorization': 'Bearer ' + access_token}
	file = system.net.httpGet(url = uri, headerValues = headers)
	
	system.perspective.download('SPM AWS.pdf',file, "application/pdf")

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 10, 2020, 3:04pm UTC](https://forum.inductiveautomation.com/t/system-perspective-download-encoding/38555/2 "2020-09-10T15:04:08Z")

</div>

The problem is probably with `httpGet`, not `system.perspective.download` - once you’re downloading the file, there’s no encoding.

Since you’re already in 8.0, I would _highly_ recommend migrating to `system.net.httpClient`.  
Unlike httpGet, it will try to use the charset from the Content-Type header in the response - and if one is not found, it falls back to UTF-8, instead of the platform’s default charset (which is UTF-8 on basically every platform _except_ Windows), as `system.net.httpGet()` does.

It would be a drop in replacement for the existing code you have:

```auto
uri = 'https://www.googleapis.com/drive/v3/files/1-fS4Bgy7NCdM8Frsed1YNOTaR_8majxm?alt=media'
headers = {'Authorization': 'Bearer ' + access_token}
file = system.net.httpClient().get(uri, headers=headers).body
# file = system.net.httpGet(url = uri, headerValues = headers)

```

---

<div class="post-metadata">

**Author:** ![victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@victor1](https://forum.inductiveautomation.com/u/victor1)\
**Post date:** [September 10, 2020, 3:21pm UTC](https://forum.inductiveautomation.com/t/system-perspective-download-encoding/38555/3 "2020-09-10T15:21:05Z")

</div>

Nice!!

Worked perfectly.  
Thank you very much!

---

<div class="post-metadata">

**Author:** ![victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@victor1](https://forum.inductiveautomation.com/u/victor1)\
**Post date:** [September 11, 2020, 4:30pm UTC](https://forum.inductiveautomation.com/t/system-perspective-download-encoding/38555/4 "2020-09-11T16:30:11Z")

</div>

@paul-griffith, I am facing a very similar problem right now, but when I upload a file, could you help me?

I’m using the ‘File upload’ component in perspective, trying to upload a file to Google Drive.  
The file arrives in the Google Drive folder, but with the wrong encoding.

onFileReceived script event:

```auto
	import mimetypes
	
	access_token = self.getSibling("txt_accessToken").props.text	
	dataList = []
	boundary = 'wL36Yn8afVp8Ag7AmP8qZ0SA4n1v9T'
	
	#Metadata file
	dataList.append('--' + boundary)
	dataList.append('Content-Disposition: form-data; name=""; filename="{0}"'.format('/usr/local/bin/ignition/webserver/webapps/main/metadata.json'))	
	fileType = mimetypes.guess_type('/usr/local/bin/ignition/webserver/webapps/main/metadata.json')[0] or 'application/octet-stream'
	dataList.append('Content-Type: {}'.format(fileType))
	dataList.append('')
	metadata = system.file.readFileAsString('/usr/local/bin/ignition/webserver/webapps/main/metadata.json')
	dataList.append(metadata)	
	
	#PDF file
	dataList.append('--' + boundary)		
	dataList.append('Content-Disposition: form-data; name=""; filename="{0}"'.format(event.file.name))	
	fileType = mimetypes.guess_type(event.file.name)[0] or 'application/octet-stream'
	dataList.append('Content-Type: {}'.format(fileType))
	dataList.append('')
	dataList.append(event.file.getString())
		  
	dataList.append('--'+boundary+'--')
	dataList.append('')
	
	body = '\r\n'.join(dataList)
	payload = body
	
	self.getSibling("txt_requestBody").props.text = payload
	
	uri = 'https://www.googleapis.com/upload/drive/v3/files'
	headers = {'Content-Type': 'application/json; charset=UTF-8', 'Authorization': 'Bearer ' + access_token, 'Content-type': 'multipart/form-data; boundary={}'.format(boundary)}
	
	resposnse = system.net.httpClient().post(uri, data=payload, headers=headers)
	
	logger = system.util.getLogger("myLogger")
	logger.info(str(resposnse))

```

Original PDF file opened in Notepad:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/f/9f62fd3ac5e3e53cae5de9c5b5620b19e7f0a8d1.png)

Uploaded file:

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/c/e/ce51974586ac635002ecf8c4ee6e1e52f1e7fe8d.png)

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 11, 2020, 4:53pm UTC](https://forum.inductiveautomation.com/t/system-perspective-download-encoding/38555/5 "2020-09-11T16:53:51Z")

</div>

> [@victor1](#):
>
> dataList.append(event.file.getString())

Is implicitly using the default charset. Add `"UTF-8"` to the `getString()` call.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 11, 2020, 5:04pm UTC](https://forum.inductiveautomation.com/t/system-perspective-download-encoding/38555/6 "2020-09-11T17:04:16Z")

</div>

Why are you not using .getBytes() if you are just saving the file to the filesystem? Converting from bytes to string back to bytes is not guaranteed to be idempotent.

---

<div class="post-metadata">

**Author:** ![victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@victor1](https://forum.inductiveautomation.com/u/victor1)\
**Post date:** [September 11, 2020, 5:08pm UTC](https://forum.inductiveautomation.com/t/system-perspective-download-encoding/38555/7 "2020-09-11T17:08:33Z")

</div>

I added the charset parameter, but I’m still getting the encoding error

```auto
dataList.append(event.file.getString("UTF-8"))

```

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/f/afde91f5680e69d1a8600c485021382b41ee82fe.png)

---

<div class="post-metadata">

**Author:** ![victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@victor1](https://forum.inductiveautomation.com/u/victor1)\
**Post date:** [September 11, 2020, 5:11pm UTC](https://forum.inductiveautomation.com/t/system-perspective-download-encoding/38555/8 "2020-09-11T17:11:15Z")

</div>

But I am not saving to the file system.  
I’m trying to upload the file via HTTP to the Google Drive API

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 11, 2020, 5:15pm UTC](https://forum.inductiveautomation.com/t/system-perspective-download-encoding/38555/9 "2020-09-11T17:15:59Z")

</div>

You should be constructing your upload with binary (probably base64) encoding of the bytes, not a string. PDFs have binary content and will not decode/reencode properly as strings.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 11, 2020, 5:59pm UTC](https://forum.inductiveautomation.com/t/system-perspective-download-encoding/38555/10 "2020-09-11T17:59:42Z")

</div>

Ah, yeah, Phil’s right. You need to retrieve the bytes and encode them yourself into base64 to place into the `multipart/form-data` you’re building.

---

<div class="post-metadata">

**Author:** ![victor1](https://avatars.discourse-cdn.com/v4/letter/v/6de8d8/32.png) [@victor1](https://forum.inductiveautomation.com/u/victor1)\
**Post date:** [September 11, 2020, 6:28pm UTC](https://forum.inductiveautomation.com/t/system-perspective-download-encoding/38555/11 "2020-09-11T18:28:27Z")

</div>

I made the following attempts:

```auto
dataList.append(base64.b64encode(event.file.getBytes()))	

```

```auto
dataList.append(base64.b64decode(event.file.getBytes()))

```

```auto
dataList.append(base64.b64encode(event.file.getBytes()).encode('utf-8'))	

```

```auto
dataList.append(base64.b64encode(event.file.getBytes()).decode('utf-8'))

```

```auto
dataList.append(base64.b64decode(event.file.getBytes()).encode('utf-8'))

```

```auto
dataList.append(base64.b64decode(event.file.getBytes()).decode('utf-8'))

```

but none of them worked …
