cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
jmikalik
Engaged Sweeper

Hello everyone.

I am trying to figure out how to upload a file to the lansweeper API using XMLHttpRequest. I am unsure what I need to do but this is what I for code inside my data submitting function:

 

 

	var xhttp = new XMLHttpRequest();
	
	var objFile = document.getElementsByName("FormFile")[0].Files[0];
	
	var Data = new FormData();
	
	Data.append("File", objFile);
	
	xhttp.open("POST", API_URL);
	xhttp.send(Data);

 

 

So far, I can get a ticket created using the API URL string, but I am stuck on the hurdle of getting the File attached to a new ticket. Any help with this would be greatly appreciated.

Thanks,

Jimmy

 

2 REPLIES 2
jmikalik
Engaged Sweeper

I have figured out what my issue is. I was creating the html file on my local PC and testing it there, but to upload the file using the ticket API I was getting a CORs policy error. The fix was to place the file directly on the server and access the html file from the Server's URL path. 

I am inexperienced when it comes to APIs as well as coding in general. I did not fully understand how to send the information and decided to build the function in Javascript. For any that may be in the same boat as me, here is the function snippet I used to create a ticket using the API.

async function submitData() {

//placed this alert to confirm the function is firing off
alert("Ticket Submitted");

//using a html form to get the information from the user and on submit this function places the data into these variables

var Subject = document.getElementsByName("FormSubject")[0].value;
var Description = document.getElementsByName("FormDescription")[0].value;
var Email = documnet.getElementsByName("FormEmail")[0].value;

//placed the server API string into a variable to easily send data later. also added the variables that were created earlier to the string

const api_url = "serverapi.aspx?Action=AddTicket&Key=keystring&Subject=" + Subject + "&Description=" + Description + "&Email=" + Email

//store the XMLHttpRequest into a variable
var xhttp = new XMLHttpRequest();

//Store an attached file from a form if needed
var objFile = document.getElementById("IDfile").files[0];

//creating a form data to submit the file to the API
var Data = new FormData();
Data.append("Files", objFile);

//calling the XMLHttpRequest to send the data to API also added some progress statements which ultimately made me realize my console was not keeping data logged when the page refreshed. This then lead to me realizing I was getting the CORS Policy error

xhttp.open("POST", api_url);
console.log('OPENED: ', xhttp.status);
xhttp.onprogress = () => {
console.log('LOADING: ', xhttp.status);
};

xhttp.onload = () => {
console.log('DONE: ', xhttp.status);
};
	
xhttp.send(Data);
}

 As I stated before I don't know much about coding, so If anyone has advice on how this could be done I am open to suggestions. Thank you.

Obi_1_Cinobi
Lansweeper Tech Support
Lansweeper Tech Support

Hello there,

The complete API documentation, including some sample code for sending attachments, can be found in the Lansweeper web console under Configuration/Ticket API.

New to Lansweeper?

Try Lansweeper For Free

Experience Lansweeper with your own data.
Sign up now for a 14-day free trial.

Try Now