Javascript FileReader onload not firing

前端 未结 4 839
故里飘歌
故里飘歌 2020-12-31 03:02

I haven\'t used JavaScript in a while and I can\'t seem to read a text file and display the contents.

I\'ve tried onload as well as onloadend

4条回答
  •  旧巷少年郎
    2020-12-31 03:40

    Nothing worked for me. Below you can find my ugly solution, but it was the only one that did the job. In my case the user can upload up to 3 files, so the var iPDF can be 0,1,2.

    	var iPDF=UpdatedExistingNumberOfPDFfiles;
    	if (iPDF < NoMaxPDFfiles) {
    		var reader = new FileReader();
    		reader.readAsDataURL(files[iPDF-UpdatedExistingNumberOfPDFfiles]);
    		reader.onload = function (){
    		var PDFdataURL = reader.result;
    		var xhr = new XMLHttpRequest();
    		xhr.open("POST", "Observation_PDFUpload.php",true);
    		xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    		var NumberOfPDFfile = iPDF+1;
    		xhr.send("PDFfileURL="+PDFdataURL+"&PDFfileName="+PDFfileName+"-Annex-"+NumberOfPDFfile);
    		iPDF++;
    		if (iPDF < NoMaxPDFfiles) {
    			reader.readAsDataURL(files[iPDF-UpdatedExistingNumberOfPDFfiles]);
    			reader.onload = function (){
    			PDFdataURL = reader.result;
    			var xhr1 = new XMLHttpRequest();
    			xhr1.open("POST", "Observation_PDFUpload.php",true);
    			xhr1.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    			NumberOfPDFfile = iPDF+1;
    			xhr1.send("PDFfileURL="+PDFdataURL+"&PDFfileName="+PDFfileName+"-Annex-"+NumberOfPDFfile);
    			iPDF++;	
    			if (iPDF < NoMaxPDFfiles) {
    			reader.readAsDataURL(files[iPDF-UpdatedExistingNumberOfPDFfiles]);
    			reader.onload = function (){
    			PDFdataURL = reader.result;
    			var xhr2 = new XMLHttpRequest();
    			xhr2.open("POST", "Observation_PDFUpload.php",true);
    			xhr2.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    			NumberOfPDFfile = iPDF+1;
    			xhr2.send("PDFfileURL="+PDFdataURL+"&PDFfileName="+PDFfileName+"-Annex-"+NumberOfPDFfile);
    			}	
    			}			
    		
    			}
    		}
    		}
    		};

提交回复
热议问题