Nick
Nick

Reputation: 343

Shiny file Input accepting all file types

I have created a form on Shiny which has fileInput button to upload only .pdf extension file.But while testing it i found that it's accepting all kind of file type however i have mentioned .pdf in accept parameter of fileInput shown in below codes:

 fileInput("fileid","Upload .pdf file only",multiple = FALSE, accept = c('.pdf'),width = "250px")

screenshot below that accepts all kind of file type:

accepting .xls file type

enter image description here

Accepting .csv file type:

enter image description here

I am looking for a solution that restrict user to upload .pdf file only and won't allow user to upload if some other file is selected.

Any help would be appreciated. :)

Upvotes: 2

Views: 1397

Answers (1)

Jan
Jan

Reputation: 5254

You can and must verify that yourself in your server function. fileInput gives you the MIME type and you can validate that has the correct type before you load it.

output$DisplayFileContent <- renderPrint({
  req(input$fileid)
  # Check for file type
  if (input$fileid$type != "application/pdf") stop("No PDF")
  
  pdffile <- readBin(con=input$file_input$datapath, what = 'raw',n=input$file_input$size)
  # ... more code to show file content
})

Upvotes: 5

Related Questions