I am trying to upload a file using XMLHttpRequest() but the post request is returning a 500 internal server error. I've made sure the file parameter is sending through the file object and that the action URL is correct. Am I missing something?
HTML:
<input type="file" class="form-control" name="documents" (change)="onFileUploadChange($event)">
Component:
onFileUploadChange(_event: any) {
let file = _event.srcElement.files;
let postData: any = null;
this._fileUploadService.uploadFile(this.uploadURL, file);
}
Service:
uploadFile(_url:string,_file:File):Promise<any> {
return new Promise((resolve, reject) => {
var xhr:XMLHttpRequest = new XMLHttpRequest();
console.log(_file);
console.log(_file[0].name);
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
// ...
} else if (xhr.status === 500) {
// ...
}
else {
// ...
}
}
};
var formData = new FormData();
formData.append('file', _file[0], _file[0].name);
xhr.open('POST', _url, true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Content-Type','multipart/form-data');
xhr.send(formData);
});
}