Jump to content

Require Specific File format On File Upload Field


Recommended Posts

I am trying to only allow specific file type upload using JS 

<SCRIPT LANGUAGE="JavaScript">
document.getElementById("Files").setAttribute("accept","application/pdf,application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
</SCRIPT>

Where 'Files' is the table field name. But it still allows me to upload a PNG file. Am i doing something wrong?

Link to comment
Share on other sites

Hi DavidKennett,

I have used following JS in my Tablular Report Datapage (inline insert enabled) which hides/disables the "Add" button if uploaded file has any other extension except the following: .pdf, .doc, .docx, .xls, .xlsx.

<SCRIPT LANGUAGE="JavaScript">

var v_file = document.getElementById('InlineAddPhoto');
var v_btn = document.getElementById('Mod0InlineAdd');

v_file.addEventListener('change', function(v_e){
 var v_ext = (v_file.value || '').split('.').pop().toLowerCase();
 if (!['pdf', 'doc', 'docx','xls','xlsx'].contains(v_ext)){
 v_btn.disabled;
 alert('Please attach a document with following extension: .pdf, .doc, .docx, .xls, .xlsx'); // pop-up alert message 
 v_btn.style.display="none";}

 else {v_btn.style.display="inline-block";}
});
</SCRIPT>

Please paste this snippet of code into the Footer.

Hope this helps.

 

Regards,

vitalikssssss

 

Link to comment
Share on other sites

  • 3 years later...
  • 4 months later...
  • 2 months later...

Hi - Just wanted to share this solution. If you want to restrict file type in multiple fields, just like in this article: https://howto.caspio.com/tech-tips-and-articles/advanced-customizations/restrict-file-types-that-can-be-uploaded-in-a-datapage/

You can use this code:

<script type="text/javascript">

var files = Array.from(document.querySelectorAll('#InsertRecordFIELDNAME1, #InsertRecordFIELDNAME2, #InsertRecordFIELDNAME3, #InsertRecordFIELDNAME4, #InsertRecordFIELDNAME5'));

var extensions = ['pdf'];

files.forEach(function(el) {
el.addEventListener('change', function(e) {

var ext = this.value.match(/.([^.]+)$/)[1];

if (!extensions.includes(ext)){

alert('Invalid file format. Please upload PDF only.');

this.value = '';

}
});});

</script>

Hope it helps!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...