diff --git a/dom/html/HTMLInputElement.cpp b/dom/html/HTMLInputElement.cpp
index aaca0823c4f..ecad501e664 100644
--- a/dom/html/HTMLInputElement.cpp
+++ b/dom/html/HTMLInputElement.cpp
@@ -295,20 +295,25 @@ NS_IMPL_ISUPPORTS(UploadLastDir::ContentPrefCallback, nsIContentPrefCallback2)
NS_IMETHODIMP
UploadLastDir::ContentPrefCallback::HandleCompletion(uint16_t aReason)
{
- nsCOMPtr localFile = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID);
- NS_ENSURE_STATE(localFile);
+ nsCOMPtr localFile;
+ nsAutoString prefStr;
- if (aReason == nsIContentPrefCallback2::COMPLETE_ERROR ||
- !mResult) {
- // Default to "desktop" directory for each platform
- nsCOMPtr homeDir;
- NS_GetSpecialDirectory(NS_OS_DESKTOP_DIR, getter_AddRefs(homeDir));
- localFile = do_QueryInterface(homeDir);
- } else {
- nsAutoString prefStr;
- nsCOMPtr pref;
- mResult->GetValue(getter_AddRefs(pref));
- pref->GetAsAString(prefStr);
+ if (aReason == nsIContentPrefCallback2::COMPLETE_ERROR || !mResult) {
+ prefStr = Preferences::GetString("dom.input.fallbackUploadDir");
+ if (prefStr.IsEmpty()) {
+ // If no custom directory was set through the pref, default to
+ // "desktop" directory for each platform.
+ NS_GetSpecialDirectory(NS_OS_DESKTOP_DIR, getter_AddRefs(localFile));
+ }
+ }
+
+ if (!localFile) {
+ if (prefStr.IsEmpty() && mResult) {
+ nsCOMPtr pref;
+ mResult->GetValue(getter_AddRefs(pref));
+ pref->GetAsAString(prefStr);
+ }
+ localFile = do_CreateInstance(NS_LOCAL_FILE_CONTRACTID);
localFile->InitWithPath(prefStr);
}
diff --git a/dom/html/test/mochitest.ini b/dom/html/test/mochitest.ini
index 340a2654481..c25fef89f41 100644
--- a/dom/html/test/mochitest.ini
+++ b/dom/html/test/mochitest.ini
@@ -600,3 +600,5 @@ skip-if = buildapp == 'b2g' # bug 1129014
[test_image_clone_load.html]
[test_bug1203668.html]
[test_bug1166138.html]
+[test_filepicker_default_directory.html]
+skip-if = buildapp == 'mulet' || buildapp == 'b2g' || toolkit == 'android'
diff --git a/dom/html/test/test_filepicker_default_directory.html b/dom/html/test/test_filepicker_default_directory.html
new file mode 100644
index 00000000000..c2212baa04b
--- /dev/null
+++ b/dom/html/test/test_filepicker_default_directory.html
@@ -0,0 +1,83 @@
+
+
+
+
+ Test for filepicker default directory
+
+
+
+
+
+Mozilla Bug 1194893
+
+
+
+
+
+
+
+
diff --git a/modules/libpref/init/all.js b/modules/libpref/init/all.js
index c7803e14045..291084294bd 100644
--- a/modules/libpref/init/all.js
+++ b/modules/libpref/init/all.js
@@ -5131,3 +5131,6 @@ pref("dom.mozKillSwitch.enabled", false);
pref("toolkit.pageThumbs.screenSizeDivisor", 7);
pref("toolkit.pageThumbs.minWidth", 0);
pref("toolkit.pageThumbs.minHeight", 0);
+
+// Allow customization of the fallback directory for file uploads
+pref("dom.input.fallbackUploadDir", "");