diff --git a/IPython/html/files/handlers.py b/IPython/html/files/handlers.py
index 11607f000..7ba667f78 100644
--- a/IPython/html/files/handlers.py
+++ b/IPython/html/files/handlers.py
@@ -6,6 +6,7 @@
import logging
import os
import mimetypes
+import json
try:
# py3
from http.client import responses
@@ -34,7 +35,9 @@ class FilesHandler(IPythonHandler):
raise web.HTTPError(404)
path, name = os.path.split(path)
- if name.endswith('.ipynb'):
+ model = cm.get_model(name, path)
+
+ if model['type'] == 'notebook':
self.set_header('Content-Type', 'application/json')
else:
cur_mime = mimetypes.guess_type(name)[0]
@@ -42,6 +45,12 @@ class FilesHandler(IPythonHandler):
self.set_header('Content-Type', cur_mime)
self.set_header('Content-Disposition','attachment; filename="%s"' % name)
- self.write(cm.get_model(name, path)['content'])
+
+ if model['format'] == 'base64':
+ self.write(model['content'].decode('base64'))
+ elif model['format'] == 'json':
+ self.write(json.dumps(model['content']))
+ else:
+ self.write(model['content'])
self.flush()
diff --git a/IPython/html/tests/test_files.py b/IPython/html/tests/test_files.py
index bd3eeedae..53383bda2 100644
--- a/IPython/html/tests/test_files.py
+++ b/IPython/html/tests/test_files.py
@@ -76,7 +76,7 @@ class FilesTest(NotebookTestBase):
write(nb, f, format='ipynb')
with io.open(pjoin(nbdir, 'test.bin'), 'wb') as f:
- f.write(b'\xFF' + os.urandom(5))
+ f.write(b'\xff' + os.urandom(5))
f.close()
with io.open(pjoin(nbdir, 'test.txt'), 'w') as f:
@@ -91,7 +91,9 @@ class FilesTest(NotebookTestBase):
r = requests.get(url_path_join(base, 'files', 'test.bin'))
self.assertEqual(r.status_code, 200)
self.assertEqual(r.headers['content-type'], 'application/octet-stream')
- self.assertTrue(r.content.startswith(b'\xFF'))
+
+ self.assertEqual("{:02x}".format(ord(r.content[0])), 'ff')
+ self.assertEqual(len(r.content), 6)
r = requests.get(url_path_join(base, 'files', 'test.txt'))
self.assertEqual(r.status_code, 200)