Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jun 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy xmlHttpRequest response for email reader script

    If I replace the .msg file with a .txt file I can get an Alert... showing that the xmlHttpRequest is working. But I'm trying to use the xmlHttpRequest response in an email reader script. There are 3 external .js files that do the email reader processing... datastream.js, jquery-1.12.0.min.js, and msg.reader.js but the part that's giving me trouble is the code below... in the main html document. This part of the script was originally intended for a Browse button to find a local file. I'm eliminating that part and just trying to pass the file name of the emails available to view with links.

    Code:
    var xhr = new XMLHttpRequest();
    xhr.open("GET","\path\to\file.msg", true);
    xhr.send();
    xhr.onreadystatechange = processRequest;
    
    function processRequest(e) {
    //	if (xhr.readyState == 4 && xhr.status == 200) {
    	if (xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
    		
    		var data;
    		if (!xhr.responseType || xhr.responseType === "text") {
    			data = xhr.responseText;
    		} else if (xhr.responseType === "document") {
    			data = xhr.responseXML;
    		} else {
    			data = xhr.response;
    			
    			// Note: .response instead of .responseText
    			//var blob = new Blob([this.response], {type: 'text/plain'});
    			var blob = new Blob([xhr.response], {type: 'text/plain'});
    			alert(blob);
    			//alert(xhr.response);
    		}
    		alert(data); // <--- I get the Alert here when I try pulling a .txt file, but nothing for .msg
    	//	return data;
    		
    		function isSupportedFileAPI() {
    			return window.File && window.FileReader && window.FileList && window.Blob;
    		}
    		function formatEmail(data) {
    			return data.name ? data.name + " [" + data.email + "]" : data.email;
    		}
    		
    		$(function () {
    			if (isSupportedFileAPI()) {
    				$('.src-file').change(function () {
    					//var selectedFile = this.files[0]; // <--- this was from the Browse button, it was just a filename
    					//var selectedFile = xhr.responseText;
    					var selectedFile = data; // <--- I'm trying to pass the xhr.response from above, I get no errors
    					
    					alert(selectedFile.name);
    					
    					if (!selectedFile) {
    						$('.msg-info, .incorrect-type').hide();
    						return;
    					}
    					if (selectedFile.name.indexOf('.msg') == -1) {
    						$('.msg-info').hide();
    						$('.incorrect-type').show();
    						return;
    					}
    					$('.msg-example .msg-file-name').html(selectedFile.name);
    					$('.incorrect-type').hide();
    					
    					// read file...
    					var fileReader = new FileReader();
    					fileReader.onload = function (evt) {
    					
    						var buffer = evt.target.result;
    						var msgReader = new MSGReader(buffer);
    						var fileData = msgReader.getFileData();
    						if (!fileData.error) {
    							$('.msg-example .msg-from').html(formatEmail({name: fileData.senderName, email: fileData.senderEmail}));
    							
    							$('.msg-example .msg-to').html(jQuery.map(fileData.recipients, function (recipient, i) {
    								return formatEmail(recipient);
    							}).join('<br/>'));
    							$('.msg-example .msg-subject').html(fileData.subject);
    							$('.msg-example .msg-body').html(
    							fileData.body ? fileData.body.substring(0, Math.min(500, fileData.body.length))
    							+ (fileData.body.length > 500 ? '...' : '') : '');
    							$('.msg-example .msg-attachment').html(jQuery.map(fileData.attachments, function (attachment, i) {
    								return attachment.fileName + ' [' + attachment.contentLength + 'bytes]' +
    								(attachment.pidContentId ? '; ID = ' + attachment.pidContentId : '');
    							}).join('<br/>'));
    							$('.msg-info').show();
    							
    							// Use msgReader.getAttachment to access attachment content ...
    							// msgReader.getAttachment(0) or msgReader.getAttachment(fileData.attachments[0])
    						} else {
    							$('.msg-info').hide();
    							$('.incorrect-type').show();
    						}
    					};
    					fileReader.readAsArrayBuffer(selectedFile);
    				});
    			} else {
    				$('.msg-example').hide();
    				$('.file-api-not-available').show();
    			}
    		});
    	}else{
    		if(exception.name == 'NetworkError'){
    			alert('There was a network error.');
    		}
    	//	alert('HTTP error ' + xhr.status);
    	//	return;
    	}
    }

  2. #2
    New to the CF scene
    Join Date
    Jun 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    In the above script are three arrows <--- one where I have an alert, and the other two are where I'm trying to swap the browse button input with xhr.responseText msg data for the selectedFile.

  3. #3
    New to the CF scene
    Join Date
    Jun 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anybody ?

  4. #4
    Regular Coder
    Join Date
    Jun 2008
    Posts
    357
    Thanks
    6
    Thanked 27 Times in 27 Posts
    Did'nt got you. Sorry for my bad english.

    What exactly you're trying to achieve?
    I never ever read PM's unless it's an job offer. So save your time for regular questions in the forum. I never ever take friendship offers. We are not on facebook here.(It's stupid on facebook too). Friendship? Do i know you? Did we ever had a beer together? Thats really stupid. Sorry.

  5. #5
    New to the CF scene
    Join Date
    Jun 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Using link to pass value instead of Button to Browse

    A little ways down in the code, about the 38th line is where I'm having difficulty.

    var selectedFile = data; // <--- I'm trying to pass the xhr.response from above, I get no errors

  6. #6
    Master Coder sunfighter's Avatar
    Join Date
    Jan 2011
    Location
    Washington
    Posts
    7,998
    Thanks
    36
    Thanked 1,072 Times in 1,068 Posts
    This comes from MDN page: https://developer.mozilla.org/en-US/...t/responseType

    The XMLHttpRequest.responseType property is an enumerated value that returns the type of response. It also lets the author change the response type. If an empty string is set as the value of responseType, the default value text will be used.

    Setting the value of responseType to "document" is ignored if done in a Worker environment. When setting responseType to a particular value, the author should make sure that the server is actually sending a response compatible to that format. If the server returns data that is not compatible to the responseType that was set, the value of response will be null. Also, setting responseType for synchronous requests will throw an InvalidAccessError exception.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

  7. #7
    New to the CF scene
    Join Date
    Jun 2017
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Sunfighter, thank you for your reply. I'm doing something wrong here... it seemed to me like XMLHttpRequest is needed to pass the document to "var selectedFile" for it to be rendered by the email reader script. The script works fine when a Browse button is used to find and display a file from the local computer. But I'm trying to adapt this part of the script... I'm eliminating the Browse button and trying to set it up so clicking on a link will pass whatever is needed here to the email reader script so it can display an email stored on the server. As far as I could tell, this is where I need to plug in something... I tried passing the path to the email file here with XMLHttpRequest, but that didn't work either. So many people have dismissed this, saying I'm trying to Browse a server... and I'm not. I'm trying to use a link to identify a specific file and pass what is needed to the email reader script so it can display it. Each of the emails are on the server, but as you know, Javascript runs on the client. So I've been trying to see what needs to be passed to the email reader script so it will display the email in a very rudimentary layout... without the need for Outlook.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •