This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal



Sep 12, 2011, 7:00 AM
5 Posts

File Upload Control

  • Category: Server Side JavaScript
  • Platform: Windows
  • Release: 8.5.2
  • Role: Developer
  • Tags:
  • Replies: 7
HI Guys, I have an upload file control in my xpage form and I want that Upload File Control to only allow a jpeg file to be selected. How could I do that in XPage. Just like validating wheter you attached a jpeg file or not. Thanks
Sep 12, 2011, 11:20 AM
33 Posts
Re: File Upload Control
If you click on the Upload Control and select 'All Properties'... Under 'Data' you will find an option for 'Accept'.
 
Within here, you can select which values you wish to accept.  
Sep 16, 2011, 8:27 PM
5 Posts
Re: File Upload Control
Hi D,
 
 On the file upload properties Choose MIME type as Image/Jpeg and go to All Properties on the File Upload control and in the Basics tab go to the property 'accept' and select the 'value' from the options in the drop down. If the option you want for example jpg is not available then you can type that manually like image/jpg or image/jpeg.
 
Now what happens after saving the document is (it will only accept jpg images as attachments) and if any other files are attached it will ignore them and will not attach to the document, however no warning message is given.
 
Hope this helps. 
 
Regards 
Neeraj 
Oct 20, 2011, 3:50 PM
37 Posts
Re: File Upload Control
Hello,
 
   I'm trying to do the same, except with PDF's.  I want to prevent users from adding anything but PDF's in the File Upload control.  I added "application/pdf" to both the "accept" and "mime" sections, and basically, it's working as you mentioned - if the file is a PDF, the document is saved; if the file is not a PDF, the document is not saved.
 
   This is totally unacceptable for the end users.  If someone selects anything other than a PDF, the file should not be accepted.
 
   Other than scrapping the File Upload control for the easier to use jQuery File Upload plugin, how can we prevent the file from being added?
 
Thanks, in advance!
 
Steve in NYC
Oct 20, 2011, 4:47 PM
272 Posts
Re: File Upload Control
Hi Steve,
 
there is no browser available which supports a restriction of mime-types or where it is possible to limit the selectable files in dialog.
This is only possible if you use other plugins like flash or java, or whatever.
 
But keep in mind that you still have to check the uploaded file if its really the type of file it "seems to be". Limiting the selectable files by their extension is not enough, because clever users would just rename their files. On the other hand a PDF can be a PDF without having a ".pfd"-Extension...
 
Just my two cents
Sven
 
Oct 20, 2011, 5:11 PM
272 Posts
Re: File Upload Control
Here is a simple idea how you could limit files (it is just HTML Code w/o XPages integration)
 
<html>
    <head>
    <script  type="text/javascript">
        var allowedFiles = new Array("pdf","png");

        function checkForAllowedFiles( obj, allowedArray, id  ){
            try{
                var hlpName = obj.value.toLowerCase();
                var hlp = hlpName.split(".");
                var ext = hlp[hlp.length-1];
                var isOK = false;
                for( var i=0; i<allowedArray.length; i++){
                    if( allowedArray[i] == ext )
                        isOK=true;
                }

                if( isOK )
                    return true;
           
           
                document.getElementById( id ).value = '';
                alert("File not allowed!");

            }catch(e){
                alert(e);
            }
       
        }

    </script>
    </head>
    <body>
        <form action="input_file.htm" method="post" enctype="multipart/form-data">
          <p>Select file (only pdf/png)<br>
            <input id="fileUploadId" name="fileUpload" type="file" onchange="checkForAllowedFiles(this, allowedFiles,'fileUploadId');">
          </p>
        </form>
    </body>
</html>
</html>

Oct 20, 2011, 5:50 PM
37 Posts
Re: File Upload Control
Thanks, Steve!
 
   The jQuery multiFile validation works perfectly fine, so browsers can properly prevent Uploads from adding a file.
   Before I found the jQuery multiFile validation, I used javascript very similar to what you proposed.
 
   I guess my bigger question is why is it so simple in traditional Notes/Domino, easier in jQuery, but messy with XPages?  Isn't XPages the supposed "fastest" way to building apps?  So, why does the "accept" property only seem to work on document submit and not when actually selecting a file.  Are users supposed to just accept this behavior?  Doesn't make sense.
 
   At this point, maybe I'll just scrap XPages until it becomes more mature, unless someone can help me figure this out.
 
Cheers!
 
Steve in NYC
 
"It hurts to be on the cutting edge."
Oct 20, 2011, 7:01 PM
272 Posts
Re: File Upload Control
Hi Steve,
 
I think there is a missunderstanding about XPages and their options, because this a an browser issue, and will never be fixed... Since IE 3.0 / Netscape 2.0 there is a property "accept" for the fileupload tag in HTML, but it was never ever implemented by any browser:
 
<input name="File" type="file" size="50" maxlength="100000" accept="text/*">
 
Please have a look here: dojox.form.FileUploader. It's the same as in jQuery as in other JS Frameworks. But theres always the same problem: If you want to stop users to upload ".exe" files, it does require more than checking the file extension; if a user renames f.e. "Virus.exe" to "Virus.pdf", he is able to upload this file. This is why the mimetype has to be checked on server side. The XPages provides the serverside validation of the uploaded file.
 
Sven

This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal