-1

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);
    });
}
skyscript
  • 175
  • 2
  • 12

2 Answers2

0

A 500 error means the request got to the destination and there was a problem at the destination. Try checking the destination for errors.

JordRoss
  • 72
  • 3
  • 8
-1

Maybe you send wrong request? Last time I've got the same problem and there was a solution:

Saving Blob as xlsx in Angular2

Artur Czopek
  • 292
  • 1
  • 9