Visual Builder

Get Involved. Join the Conversation.

Topic

    Muralidharan Kanagaraj
    How to download uploaded file in button action?
    Topic posted September 3, 2019 by Muralidharan KanagarajRed Ribbon: 250+ Points, tagged Business Objects, Custom Code, Database, UI 
    78 Views, 4 Comments
    Title:
    How to download uploaded file in button action?
    Content:

    Hi All,

    We have file upload functionality in our VBCS page. 
    Uploaded file will be of in any format i.e (PDF, txt, Document, excel, image etc.) we show that file in table as shown in below image.
    We get the upoladed file as a link(URL) in a column and we display the link as a button for the good UI. When we click that button, that uploaded file gets downloaded.
    i.e when we click a button that uploaded file has to be downloaded.

    Is it possible to downolad the file in the same uploaded format?

    Thanks

    Version:
    19.1.3
    Image:

    Comment

     

    • Duncan Mills

      This will all depend on where you are uploading the file too and how it will give you the file back - if for example the file is accessible via a GET request as a blob from the back end server then you can use window.URL.createObjectURL(blob) to create a link once you have that you can programatically trigger a download from a button. Here's a page function that does that kind of thing:

        PageModule.prototype.saveToFile = function (bytes, fileName, downloadElementId) {
          var anchor = document.getElementById(downloadElementId);
          if (anchor) {
            var dataURL = window.URL.createObjectURL(bytes);
            anchor.href = dataURL;
            anchor.download = fileName;
            anchor.click();
            window.URL.revokeObjectURL(dataURL);
          }
        }
      

      downloadElementId is just the ID of an invisible anchor (a) tag on the page 

       

      • Muralidharan Kanagaraj

        Hi Duncan

        Thanks for your reply. We are using storage cloud to store our files.

        Thanks

      • Shakila Murugan

        Hi Duncan,

        I tried this one its working fine, but only for word and excel  files. these two only downloaded but other format of files just open in same tab.

        How to make all format of files goes to download?

        Kindly help us.

        Regards,

        Shakila

    • Duncan Mills

      That may be the mime-type mappings on your machine? On windows (when I used to use it many years ago) the registry defines the default behavior of clicking a download link for a given file type in the browser - this may no longer be the case though. It could also be a browser plug-in getting in the way (for PDF for example) 

      In theory the download parameter we are setting on the anchor tag should hint to the browser that a download is required but IE11 for example does not support that.

      With the code above we are just clicking the link on behalf of the user so it's doing the default action for that browser / machine.  It might make things better if you add anchor.target = "_blank" to the above code so that at least if the file is opened in Browser then it will be in a separate tab.

      Apart from that if this is a real problem then adapt the idea above and once you have created the dataUrl assign that as the src of a link that is actually shown to the user so the user has the option of right-mouse clicking and doing "save-as" if the default click does not actually download. So you'll end up with a link not a button but no big deal as the link can be styled