How to make input type= file Should accept only pdf and xls

后端 未结 9 2112
一生所求
一生所求 2020-12-13 11:26

I used

Now I would like to restrict this by accepting only .pdf and .xls files.

When I click the s

相关标签:
9条回答
  • 2020-12-13 12:06

    You can try following way

    <input type= "file" name="Upload" accept = "application/pdf,.csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel">
    

    OR (in asp.net mvc)

    @Html.TextBoxFor(x => x.FileName, new { @id = "doc", @type = "file", @accept = "application/pdf,.csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" })
    
    0 讨论(0)
  • 2020-12-13 12:06

    If you want the file upload control to Limit the types of files user can upload on a button click then this is the way..

    <script type="text/JavaScript">
    <!-- Begin
    function TestFileType( fileName, fileTypes ) {
    if (!fileName) return;
    
    dots = fileName.split(".")
    //get the part AFTER the LAST period.
    fileType = "." + dots[dots.length-1];
    
    return (fileTypes.join(".").indexOf(fileType) != -1) ?
    alert('That file is OK!') : 
    alert("Please only upload files that end in types: \n\n" + (fileTypes.join(" .")) + "\n\nPlease select a new file and try again.");
    }
    // -->
    </script>
    

    You can then call the function from an event like the onClick of the above button, which looks like:

    onClick="TestFileType(this.form.uploadfile.value, ['gif', 'jpg', 'png', 'jpeg']);"

    You can change this to: PDF and XLS

    You can see it implemented over here: Demo

    0 讨论(0)
  • 2020-12-13 12:08

    You could do so by using the attribute accept and adding allowed mime-types to it. But not all browsers do respect that attribute and it could easily be removed via some code inspector. So in either case you need to check the file type on the server side (your second question).

    Example:

    <input type="file" name="upload" accept="application/pdf,application/vnd.ms-excel" />
    

    To your third question "And when I click the files (PDF/XLS) on webpage it automatically should open.":

    You can't achieve that. How a PDF or XLS is opened on the client machine is set by the user.

    0 讨论(0)
  • 2020-12-13 12:08

    Try this one:-

                  <MyTextField
                    id="originalFileName"
                    type="file"
                    inputProps={{ accept: '.xlsx, .xls, .pdf' }}
                    required
                    label="Document"
                    name="originalFileName"
                    onChange={e => this.handleFileRead(e)}
                    size="small"
                    variant="standard"
                  />
    
    0 讨论(0)
  • 2020-12-13 12:10

    Unfortunately, there is no guaranteed way to do it at time of selection.

    Some browsers support the accept attribute for input tags. This is a good start, but cannot be relied upon completely.

    <input type="file" name="pic" id="pic" accept="image/gif, image/jpeg" />
    

    You can use a cfinput and run a validation to check the file extension at submission, but not the mime-type. This is better, but still not fool-proof. Files on OSX often have no file extensions or users could maliciously mislabel the file types.

    ColdFusion's cffile can check the mime-type using the contentType property of the result (cffile.contentType), but that can only be done after the upload. This is your best bet, but is still not 100% safe as mime-types could still be wrong.

    0 讨论(0)
  • 2020-12-13 12:11

    I would filter the files server side, because there are tools, such as Live HTTP Headers on Firefox that would allow to upload any file, including a shell. People could hack your site. Do it server site, to be safe.

    0 讨论(0)
提交回复
热议问题