How to download only a PDF file

I want to download a pdf file, but not a doc file, if I use javascript code, it will accept a doc file as well

function CheckFile() { var file = document.getElementById('FileUpload1'); var len=file.value.length; var ext=file.value; if (file.value.length <= 0) { alert('Please select a file to import'); document.getElementById('FileUpload1').focus(); return false; } else if (ext.substr(len-3,len)!="pdf" ) { alert("Please select a pdf file "); return false; } } 
0
source share
5 answers

You can use jquery or javascript as you wish.

Example for asp control:

 <asp:FileUpload ID="PDFFiles" runat="server" CssClass="form-control" AllowMultiple="true"/> 

The document is ready:

JQuery:

 $(document).ready(function () { $('#' + "<%=PDFFiles.ClientID%>").attr('accept', 'application/pdf'); }) 

Javascript:

 $(document).ready(function () { document.getElementById("<%=PDFFiles.ClientID %>").setAttribute('accept', 'application/pdf'); }) 
+2
source share

There is no prefect solution for checking the PDF file, but what you can do is (server side):

  • Confirm extensions (pdf, doc, docx) * almost useless
  • Confirm MIME
  • Open the PDF file, read the title (first line) and check if it contains one of the following lines: % PDF-1.0,% PDF-1.1,% PDF-1.2,% PDF-1.3,% PDF-1.4

As an extra, I also check if the file contains a file string indicating the number of pages by searching for multiple " / Page "

+1
source share

Are you actually loading with FileUploadControl? If so, check the server check (re: @Prisoner @WestDiscGolf), for example:

 if (fileUploadControl.PostedFile.ContentType.Contains("pdf")) { //continue processing } 

Pdf MIME types should be "application / pdf", but you often get "x-pdf". Therefore, contains.

0
source share

You can directly use asp code

 <asp:FileUpload ID="FileUpload1" runat="server" Height="26px" accept=".pdf" /><br /> <asp:RequiredFieldValidator ID="RequiredFieldValidator4" ForeColor="Red" runat="server" ValidationGroup="gr1" ControlToValidate="FileUpload1">pdf File Required</asp:RequiredFieldValidator> <asp:RegularExpressionValidator ID="regval1" ForeColor="Red" runat="server" ValidationGroup="gr1" ControlToValidate="FileUpload1" ValidationExpression="^([a-zA-Z].*|[1-9].*)\.(((p|P)(d|D)(f|F)))$">Only Accept pdf </asp:RegularExpressionValidator> 
0
source share

This works for me:

 <asp:FileUpload ID="uploadPDF" ClientIDMode="static" runat="server" accept=".pdf" /> 
0
source share

All Articles