闪亮的文件输入接受所有文件类型

Shiny file Input accepting all file types

我在 Shiny 上创建了一个表单,其中有 fileInput 按钮可以仅上传 .pdf 扩展名 file.But 在测试它时我发现它接受所有类型的文件但是我在接受参数中提到了 .pdf fileInput 显示在下面的代码中:

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

下面的屏幕截图接受所有类型的文件:

接受 .xls 文件类型

接受 .csv 文件类型:

我正在寻找一种解决方案,限制用户只能上传 .pdf 文件,如果选择了其他文件则不允许用户上传。

如有任何帮助,我们将不胜感激。 :)

您可以而且必须在您的服务器功能中验证您自己。 fileInput 为您提供 MIME 类型,您可以在加载它之前验证其类型是否正确。

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
})