diff --git a/file.php b/file.php
index 994b77a0c2256a0218a27ffebb01b1c7d77a344b..4a2ce7962788c2ce0fe4f5f9b8c3af411d6ea1cd 100644
--- a/file.php
+++ b/file.php
@@ -28,10 +28,11 @@ if (!$_GET['key']
 
 // Get the object type the file is attached to
 $type = '';
+$attachment = null;
 if ($_GET['id']
-        && ($a=$file->attachments->findFirst(array(
+        && ($attachment=$file->attachments->findFirst(array(
                     'id' => $_GET['id']))))
-    $type = $a->type;
+    $type = $attachment->type;
 
 // Enforce security settings if enabled.
 if ($cfg->isAuthRequiredForFiles()
@@ -62,7 +63,8 @@ if ($file->verifySignature($_GET['signature'], $_GET['expires'])) {
             return $file->display($s);
 
         // Download the file..
-        $file->download(@$_GET['disposition'] ?: false, $_GET['expires']);
+        $filename = $attachment ? $attachment->name : $file->getName();
+        $file->download(@$_GET['disposition'] ?: false, $_GET['expires'], $filename);
     }
     catch (Exception $ex) {
         Http::response(500, 'Unable to find that file: '.$ex->getMessage());
diff --git a/include/class.file.php b/include/class.file.php
index d880840861b09065a34cd275ebb726773d82e105..4d67b93ae5b1f8645fd9328415d0272da0cd2a64 100644
--- a/include/class.file.php
+++ b/include/class.file.php
@@ -248,7 +248,7 @@ class AttachmentFile extends VerySimpleModel {
         return hash_hmac('sha1', implode("\n", $pieces), SECRET_SALT);
     }
 
-    function download($disposition=false, $expires=false) {
+    function download($disposition=false, $expires=false, $name=false) {
         $disposition = $disposition ?: 'inline';
         $bk = $this->open();
         if ($bk->sendRedirectUrl($disposition))
@@ -258,7 +258,7 @@ class AttachmentFile extends VerySimpleModel {
         $type = $this->getType() ?: 'application/octet-stream';
         if (isset($_REQUEST['overridetype']))
             $type = $_REQUEST['overridetype'];
-        Http::download($this->getName(), $type, null, 'inline');
+        Http::download($name ?: $this->getName(), $type, null, 'inline');
         header('Content-Length: '.$this->getSize());
         $this->sendData(false);
         exit();
diff --git a/include/class.forms.php b/include/class.forms.php
index 760ea76d051175783a240ac1a03e1a9912457e87..32a0cd9e23813cd009ce343852f06b8cb87b8f5d 100644
--- a/include/class.forms.php
+++ b/include/class.forms.php
@@ -3472,9 +3472,7 @@ class FileUploadField extends FormField {
     }
 
     function parse($value) {
-        // Values in the database should be integer file-ids
-        return array_map(function($e) { return (int) $e; },
-            $value ?: array());
+        return $value;
     }
 
     function to_php($value) {
diff --git a/include/class.thread.php b/include/class.thread.php
index 59d864060708003ca72a0439fef703752551fc62..bb65c26f61e4e1048bb72ad14759cca063611a54 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -1085,12 +1085,12 @@ implements TemplateVariable {
         foreach ($files as $id => $info) {
             $F = array('inline' => is_array($info) && @$info['inline']);
 
-            if (is_numeric($id) && $id != 0)
+            if (is_array($info) && isset($info['id']))
+                $fileId = $info['id'];
+            elseif (is_numeric($id) && $id != 0)
                 $fileId = $id;
             elseif ($info instanceof AttachmentFile)
                 $fileId = $info->getId();
-            elseif (is_array($info) && isset($info['id']))
-                $fileId = $info['id'];
             elseif ($AF = AttachmentFile::create($info))
                 $fileId = $AF->getId();
             elseif ($add_error) {