Download file(s) from Media
# help-with-umbraco
j
Hi! What would be the best way (/does anyone have a snippet) for downloading a file from the Media library, given that we've identified the URL for that file - i.e.
var myMediaUrl = "/media/k3sd2q2z/myFile.pdf"
. Intended use is for a Controller and/or button form submission (i.e. press the Download button, download the file). Bonus points for having multiple media file URLs, compressing them all into one .ZIP file and downloading that .ZIP file. TIA! Rich Umbraco v13
a
I would do it something like the snippet on the link below (Discord wants 💰 💰 💰 to show the entire snippet here, so I've put it on GitHub instead): https://gist.github.com/abjerner/18997a4dcdc65d2dab94f2904301638b A few pointers: - It might be a good idea to validate the path a bit if based directly on user input. I haven't looked into what kind of safe guarding the
GetMediaByPath
is doing, so not entirely sure what validation is needed. - Being a bit lazy, I hardcoded the mime type. You should probably write something to derive the mime type from the file extension.
j
Hey @Anders Bjerner - ah thank you so much that together. I'll check it out and get back to you.
s
As for zipping them up: this is very resource-intensive and I can not recommend it. It's an easy DOS attack as well, flood the server with requests to zip up files and it will soon be all over. Additionally, it is a good idea as Anders says to derive the mime type from the file extension, instead of reading the file to determine it's type. Again, that's would be an easy way to overwhelm the server.
a
Not sure about your use case for the ZIP part, but this can also be handled in JavaScript (aka client side). We've done this for a recent project for the very same reason that Seb mentions.
j
The use case is a music-based one. I have many songs. Each song has many parts (a PDF file per instrument - i.e. "Trumpet1.pdf"). I want a way to create setlists of songs (i.e. an ordered list of Songs), and then if you were the specific player (i.e. Trumpet player #1) you could click and get a .ZIP folder with of your parts for all of your songs in order (i.e. the files could be renamed with [SetNumber]-[SongName]-[InstrumentName]), then .ZIPed into one easily downloadable folder.
(The music world runs on poorly organised Dropbox folders, so I'm exploring to see what I can do through Umbraco)
s
Hehe very well, but then wouldn't it make sense to pre-zip these combinations instead of zipping by request?
j
So the flexibility is needed from when there's a change to the part - imagine you've got a 10-song set, but you need to change the trumpet part for one of the songs. It could be a case of, as the admin, clicking a "Zip up setlist" button to generate those ZIP files, so that the trumpet player can then redownload the new .ZIP folder
h
As Anders mentioned you could use JavaScript, check out something like https://stuk.github.io/jszip/
s
Fair enough, I don't understand the ins and outs of music production! 😁
It is really cool this exists, so I guess this streams the files to your browser that then zips it client-side? Not sure if that is preferable to multiple file downloads.. It's more organized, for sure, but does it make sense to wait for the browser to be done with the zipping up?
h
Not delved into it's code, but it may be compressing it on the fly I think
242 Views