Forum Discussion
LauraShelley
7 years agoQrew Member
Dan, unfortunately your solution above doesn't seem to work properly with .jpg files for some reason. I'm not sure if Quickbase handles photo files differently or what...
I tried it the original way you have above, which resulted in an unreadable .jpg file named myfile.jpg.
I also tried this:
var Text fileExtension = Part([Photo], 2, ".");
"<a href='" & URLRoot() & "up/" & Dbid() & "/a/r" & [Record ID#] & "/e6/v0/' download='myfile." & $fileExtension & "'>Download Photo</a>"
This resulted in a download of the original file (not renamed). I'm using Chrome.
I tried it the original way you have above, which resulted in an unreadable .jpg file named myfile.jpg.
I also tried this:
var Text fileExtension = Part([Photo], 2, ".");
"<a href='" & URLRoot() & "up/" & Dbid() & "/a/r" & [Record ID#] & "/e6/v0/' download='myfile." & $fileExtension & "'>Download Photo</a>"
This resulted in a download of the original file (not renamed). I'm using Chrome.
- _anomDiebolt_7 years agoQrew EliteUse this version and then spontaneously break into a celebratory cubicle dance:
function download(url, fileName) {
fetch(url, {
credentials: "include"
}).then(response => response.blob())
.then(blob => {
var link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
})
}
var url = "https://haversineconsulting.quickbase.com/up/bm3ttgwp6/g/rb/eg/va/rythm.mp3";
var fileName = "Kryptonite.mp3";
download(url, fileName);
You can test this by navigating to the Pastie Database and pasting the code into the console.
Pastie Database
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=691 - _anomDiebolt_7 years agoQrew EliteBTW, the reason the download attribute does not work is because QuickBase sets the Content-Disposition header which has a higher priority in naming the file.
- LauraShelley7 years agoQrew MemberThank you, but I'm trying to use a Rich Text Formula field to create a download link, as you had indicated in your first response to the original question, so I don't think I can incorporate javascript in this case. Is there any way to rename a .jpg file for download using a Rich Text Formula field?
- LauraShelley7 years agoQrew MemberThe following solution works in a URL Formula Field. Thanks for your help, Dan! Very much appreciated!
var Text url = URLRoot() & "up/" & Dbid() & "/a/r" & [Record ID#] & "/e6/v0/";
var Text fileName = "Photo.jpg";
"javascript:fetch('" & $url & "',{credentials: 'include'}).then(response => response.blob()).then(blob => {var link = document.createElement('a'); link.href = window.URL.createObjectURL(blob); link.download = '" & $fileName & "'; link.click();})" - _anomDiebolt_7 years agoQrew EliteSo simple:
"<a href=# class='Vibrant Success' onclick=\"fetch('" & [url] & "',{credentials:'include'}).then(response=>response.blob()).then(blob=>{var link= document.createElement('a');link.href=window.URL.createObjectURL(blob);link.download='Kryptonite.mp3';link.click();})\">Download</a>"
This is a Rich Text Formula field that uses the field [url] and a hard-coded file name of Kryptonite.mp3.
It looks like this on my test page: - _anomDiebolt_7 years agoQrew EliteI rarely use the javascript protocol in a URL formula field because you can't style it as a button. If you are careful you can modify my formula to make the literal string "Kryptonite.mp3" a parameter in the same way you did with the formula variable Text fileName.
- LauraShelley7 years agoQrew MemberAh, that makes sense. Thanks again!
- _anomDiebolt_7 years agoQrew EliteThere are a lot of documentation steps that I have to go through that you never see so I can locate these solutions later.
Pastie Database (Formula)
https://haversineconsulting.quickbase.com/db/bgcwm2m4g?a=dr&rid=692