Hướng dẫn allow only pdf file upload javascript - chỉ cho phép tải lên tệp pdf javascript

Mã HTML:


React Code - Tệp đính kèm và đặt tệp ở trạng thái:

     @autobind
      private addFile[event] {
     for[var j=0;j900]{_size/=1024;i++;}
          var exactSize = [Math.round[_size*100]/100]+' '+fSExt[i];
          var date = event.target.files[0].lastModifiedDate,
          mnth = ["0" + [date.getMonth[] + 1]].slice[-2],
          day = ["0" + date.getDate[]].slice[-2];
          date=[day,mnth,date.getFullYear[]].join["/"];
          fileinformation.push[{
            "file_name":  event.target.files[j].name,
            "file_size": exactSize,
            "file_modified_date":date
          }];
          var ext = event.target.files[j].name.split['.'].pop[];
          if[ext=="pdf" || ext=="docx" || ext=="doc"|| ext=="ppt"|| ext=="pptx"]{
            
          } else{
            iscorrectfileattached=false;
          }
        }
        if[iscorrectfileattached==false]{
          alert["Only PFD, Word and PPT file can be attached."];
          return false;
        }
   this.setState[{fileinformation}];
    //new code end
    var date = event.target.files[0].lastModifiedDate,
    mnth = ["0" + [date.getMonth[] + 1]].slice[-2],
    day = ["0" + date.getDate[]].slice[-2];
    date=[day,mnth,date.getFullYear[]].join["/"];
    this.setState[{filesize:exactSize}];
    this.setState[{filedate:date}];
    //let resultFile = document.getElementById['file'];
    let resultFile = event.target.files;
    console.log[resultFile];
    let fileInfos = [];
    for [var i = 0; i < resultFile.length; i++] {
      var fileName = resultFile[i].name;
      console.log[fileName];
      var file = resultFile[i];
      var reader = new FileReader[];
      reader.onload = [function[file] {
         return function[e] {
              //Push the converted file into array
               fileInfos.push[{
                  "name": file.name,
                  "content": e.target.result
                  }];
                };
         }][file]; 
      reader.readAsArrayBuffer[file];
    }
    this.setState[{fileInfos}];
    this.setState[{FileNameValue:  event.target.files[0].name }];
    //this.setState[{IsDisabled:  true }];//for multiple file

    console.log[fileInfos];
  }

Xác thực tải lên tệp là rất quan trọng trong phát triển web để loại bỏ những kẻ gửi thư rác. Chủ yếu là những kẻ gửi thư rác hoặc bot tải lên các tệp không liên quan hoặc các tệp lớn để làm phiền trang web của bạn, nếu chúng tôi không sử dụng xác thực tệp; Đó là lý do chính để sử dụng xác thực tệp. Ở đây, chúng tôi sẽ tìm hiểu cách xác nhận phần mở rộng tệp và kích thước của tệp.

Ví dụ: & nbsp; chỉ cho phép tệp 2MB và [.pdf].





  PDF File Upload Validation
  
  
  
  
  
  

   
     
              
             
             
                                                                       
             
             
          

Danh mục: Phát triển web PHP web development php

Làm cách nào để thực hiện tệp loại đầu vào chỉ chấp nhận PDF và XLS?

Bạn có thể làm như vậy bằng cách sử dụng thuộc tính Chấp nhận và thêm các loại MIME cho phép vào nó. Nhưng không phải tất cả các trình duyệt đều tôn trọng thuộc tính đó và nó có thể dễ dàng bị xóa thông qua một số thanh tra mã. Vì vậy, trong cả hai trường hợp, bạn cần kiểm tra loại tệp ở phía máy chủ [câu hỏi thứ hai của bạn].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].

Làm thế nào để bạn chấp nhận PDF trong HTML?

Do khả năng tương thích rộng, thẻ iframe được sử dụng rộng rãi để nhúng pdf.Trình duyệt không hỗ trợ các tài liệu PDF hoặc thẻ đối tượng cũng có thể sử dụng thẻ này để nhúng mã HTML PDF.the iframe tag is widely used for embedding pdf. A browser that does not support PDF documents or the object tag can also use this tag to embed a pdf HTML code.

Làm cách nào để tải lên tệp HTML?

Xác định trường chọn tệp và nút "Duyệt" để tải lên tệp.Để xác định trường chọn tệp cho phép chọn nhiều tệp, hãy thêm nhiều thuộc tính.Mẹo: Luôn thêm thẻ cho thực tiễn tiếp cận tốt nhất! defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the tag for best accessibility practices!

Làm thế nào để bạn biết nếu một tệp là một tài liệu từ?

Để tìm loại tệp trên PC, hãy truy cập PC> Tài liệu này [hoặc bất cứ nơi nào bạn đã lưu tài liệu]> Nhấp chuột phải vào tài liệu> Chọn Thuộc tính.Ngoài ra, bạn có thể thay đổi chế độ xem của thư mục thành chi tiết và tất cả các loại điền sẽ được liệt kê.go to This PC> Documents [or wherever you saved the document]> right click on document> select properties. Alternatively you can change the view of the folder to details and all fill types will be listed.

Bài Viết Liên Quan

Chủ Đề