Hướng dẫn upload only pdf file in javascript - chỉ tải lên tệp pdf bằng 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).

 2097152)
              {
                          move_uploaded_file($file_tmp,"uploads/".$file_name);
                          $upload='
    Success! File Upload Successfully.  
';       }             else               {                           $upload='
    Error: File Extension should be .pdf or File Size less than 2MB.  
';               }                                                                    } ?>   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 để hạn chế loại tải lên tệp trong JavaScript?

Sử dụng JavaScript, bạn có thể dễ dàng kiểm tra tiện ích mở rộng tệp đã chọn với các tiện ích mở rộng tệp được phép và có thể hạn chế người dùng chỉ tải lên các loại tệp được phép. Đối với điều này, chúng tôi sẽ sử dụng hàm fileValidation (). Chúng tôi sẽ tạo hàm fileValidation () chứa mã xác thực loại tệp hoàn chỉnh.use fileValidation() function. We will create fileValidation() function that contains the complete file type validation code.

Làm thế nào để bạn tải một tệp docx lên html?

Làm thế nào để chuyển đổi Docx sang HTML..
Tải lên (các)-File (s) Chọn các tệp từ máy tính, Google Drive, Dropbox, URL hoặc bằng cách kéo nó trên trang ..
Chọn "To HTML" Chọn HTML hoặc bất kỳ định dạng nào khác bạn cần do kết quả (hơn 200 định dạng được hỗ trợ).
Tải xuống HTML của bạn ..

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