2016-07-26 2 views
-1

끌어서 놓기를 통해 CSV 파일을 업로드하려고 시도하지만 Excel 파일과 같은 다른 파일을 업로드 할 때 경고 메시지가 표시되지 않습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?문자열의 마지막 몇자를 확인하십시오.

아래 기능을 호출합니다. 실제로 CSV 파일을 업로드했는지 확인해야합니다. 나중에 파일 확장명을 확인한 후 파일을 읽고이 (CSV) 파일에있는 데이터가있는 표를 채 웁니다. CSV 이외에 다른 업로드 유형이 없다는 것이 중요합니다.

if (!file.name.endsWith == ".csv") { 

당신이 원하는 : 당신이 가지고

function handleFileSelection(evt) { 
    evt.stopPropagation(); // Do not allow the drop event to bubble. 
    evt.preventDefault(); // Prevent default drop event behavior. 

    var files = evt.dataTransfer.files; // Grab the list of files dragged to the drop box. 

    if (!files) { 
     alert("<p>At least one selected file is invalid - do not select any folders.</p><p>Please reselect and try again.</p>"); 
     return; 
    } 

    // "files" is a FileList of file objects. Try to display the contents of each file: 
    for (var i = 0, file; file = files[i]; i++) { 
     if (!file) { 
      alert("Unable to access " + file.name); 
      continue; // Immediately move to the next file object. 
     } 
     if (file.size == 0) { 
      alert("Skipping " + file.name.toUpperCase() + " because it is empty."); 
      continue; 
     } 
     if (!file.name.endsWith == ".csv") { 
      alert("Skipping " + file.name.toUpperCase() + " because it is not a known text file type."); 
      continue; 
     } 
     startFileRead(file); // Asychronously fire off a file read request. 
    } 
} 
+0

더 많은 컨텍스트와 코드를 제공해주십시오. 이 코드는 어디에서 호출하고 있습니까? 파일 배열은 어디에 정의되어 있습니까? – trincot

답변

0

문제는이 라인이다 :

if (!file.name.endsWith == ".csv") { 

the function with that name을 실행하고 싶은 반면 속성의 부정을 ".csv"와 비교하고 있습니다.

그러나이 기능은 모든 브라우저에서 사용할 수 없습니다, 그래서 당신은이 대안의 더 나은 수 있습니다 :

if (file.name.search(/\.csv$/) === -1) { 

if 블록의 코드가 실행되지 않습니다에게 !file은 항상 false으로 계산되므로 그 이유는 for 루프 (중간 표현 : file = files[i])의 루프 조건이 file이 거짓이 될 때마다 루프를 종료하기 때문입니다.

+0

고맙습니다. 작동합니다! 파일의 인코딩을 검사하는 방법이 있는지 알고 있습니까? 내 파일은 "UTF-8"이어야하며 그렇지 않은 경우 사용자에게이 사실을 알립니다. 고맙습니다! –

+0

반갑습니다. 인코딩에 관해서 : 그것은 정말로 다른 질문이며, 나는 이것이 어떻게 스스로 이루어질 수 있을지 확신하지 못합니다. 새로운 질문으로 게시하도록 요청할 수 있습니까? 그렇게하면 다른 사람들도 그것을 볼 수있게됩니다. – trincot

-2

: 여기

코드입니다

if (!file.name.endsWith(".csv")) { 
+0

작동하지 않습니다. 귀하의 예에서 좋아한다면, 내 CSV 파일은 전혀 업로드되지 않습니다. –

관련 문제