Restricting uploaded file type

Does anyone have an idea on how I could ensure the user only uploads files of a specific type?

I want to restrict the user to uploading PDF documents but, the file type column allows uploading of any(?) file type.

I tried RIGHT([_THIS],3)=PDF without success.

1 12 2,102
12 REPLIES 12

Man this one took me a while to search for. Doesnโ€™t look like it is possible yet though:

Thanks for searching this out for meโ€ฆ Itโ€™s been a year since that post and no status changes apparentlyโ€ฆ Disappointingโ€ฆ

Has someone found a workaround for that?

A bot can read the file type and could delete the wrong file and send a notification to the user. Not perfect, but maybe a solution?

https://www.googlecloudcommunity.com/gc/Feature-Ideas/Bug-File-type-column-cannot-read-the-selected-...

Hi,

I was facing the same issue. A bot in the background was no option, as users need to have direct feedback.
So, I tried a bit and found a good workaround:
My file field for the pdf may be called PDF-File.

1. Create an virtual column to the record. I called it "PDF-Check"
2. Give it a formula:
SWITCH(
RIGHT(TEXT([Receipt-PDF]),4),
".pdf","",
"","",
"PDF Files must end with .pdf !"
)
3. Put the Data Validity rule to the PDF-File: ISBLANK([PDF-Check])
4. Put the value error like "Illegal PDF File."
5. Make the PDF-Check auto-hide if empty: Show: ISNOTBLANK([PDF-Check])

By this, the PDF upload is successful whenever the PDF-Check field is empty. 
So, very simple, the PDF-Check field remains empty whenever the PDF-File is empty or correct. If not, it holds the error message from the expression in step 2.

If you place the PDF-Check right under the PDF-File in your views, it will only become visible if there is an illegal PDF uploaded. The actual safe of the record is rejected, as the PDF-File is only valid of the PDF-Check becomes empty again.

I use self-hiding virtual columns frequently to make consistency checks across multiple fields in a form. 
Hope this works for you, have fun with appsheet.

Hi @Stefan-Ried that's an awesome finding. Thank you. So it seems that we cannot do the validif directly in the file column, but we need to add an extra VC and do to validif based on this VC.

Edit: I now tested your tip but to me it's not working. It does not recognize the ".pdf".

[PDF Check] uses the expression that you use.
[File Extension uses the expression: RIGHT(TEXT([File]),4)

Fabian_Weller_0-1700656736294.png

Same here. I tried but cannot replicate this. 

Maybe I was too fast.
I figured out, that it makes actually a huge difference if you use a new-row form, OR if you declare the PDF-File as a quick-edit field. 
@Fabian_Weller 
Please try to not to edit in the *_Form view, but in the *_Detail view with the File switched as Quick-Edit field. If I edit the file as a quick-edit, I see the VCs correctly evaluated into .pdf or whatever ext your file has. If I switch to the form, I see the wrong value like you above!

I use quick edit by default, that's why I have not see this.

Now, I think I understand the issue.
@Fabian_Weller Please add a VC just displaying TEXT([PDF-File])
You will see that the detail view will show the file name.
However, !!!
the form will show the URL. This is obviously not ending with the file extension.
This is why you get this strange behavior and wrong displays of your extention.
And this is why my little trick does work in quick-edit detail views, but not with a forms. 

If you like to use the trick with new records, you need to avoid forms.
Try to combine it with another trick. Instead of the default Add-Action, which jumps to the form, you might create the new record into with a combined action of creating a new row and then opening the detail view to the new row to enter the data. See the action called "sequence of actions". ... I'll try this on the weekend, and will show the results.

If you cannot avoid forms...
The URL looks something like this and does actually include the generated filename and the extension:
https://www.appsheet.com/template/gettablefileurl?appName=....&tableName=Receipt&fileName=TABLENAME_Files_%2FILEID.FIELDNAME.20...50.pdf&appVersion=....&signature=3de....386

you could rewrite the expression to parse the extension out of the URL. 
But then the expression may distinguish between the file name and the URL case, maybe by the leading https:// pattern, so that it works in forms and detail views.

Hope this helps. 
Have fun. 

@Stefan-Ried This is only working when the file is already uploaded. It is not working if you add a new file. Even in Quick Edit. So your solution is more like a check after saving the row, or adding the file via Quick Edit. It's not possible to check the file extension while adding a new file.

Yes, unfortunately, you are right.
I found no way to avoid uploading an non-pdf file into a PDF field, in the same way it works for non-images in an image field.

The core of the issue is that appsheet creates for an image upload this html:

<input aria-label="ImageFieldname" class="FileUploadInput" type="file" name="imageInputFile" accept="image/*">

it tells most browsers, to select only image file in the file selection pop-up box.

The file-upload field simply allows all. 

<input aria-label="FileFieldname" class="FileUploadInput" type="file" name="imageInputFile" accept="*/*">

The best solution would be for most browser, if appsheet would generate the html with

         accept="application/pdf"

The simplest way would be, if appsheet adds an argument to restrict the type of files and just add the accept argument accordingly.

A really save solution for all browsers has been discussed here.
https://stackoverflow.com/questions/12142536/how-to-make-input-type-file-should-accept-only-pdf-and-...

Nevertheless, I've added this valid-if formula to my PDF-field

IF(
ISBLANK(TEXT([PDF])), true,
IF(LEFT(TEXT([PDF]),8)="https://",
CONTAINS(TEXT([PDF]),".pdf&"),
RIGHT(TEXT([PDF]),4)=".pdf"
)
)


It works for both, quick-edits in details AND forms and gives feedback, after the wrong file has been uploaded. However, if users ignore it, you have still wrong files in your upload.


@Stefan-Ried wrote:

It works for both, quick-edits in details AND forms


To me this valid if does not work in a form view.

Top Labels in this Space