【问题标题】:XMLHttpRequest file upload returning a 500 errorXMLHttpRequest 文件上传返回 500 错误
【发布时间】:2016-11-24 04:29:54
【问题描述】:

我正在尝试使用 XMLHttpRequest() 上传文件,但发布请求返回 500 内部服务器错误。我已经确定文件参数是通过文件对象发送的,并且操作 URL 是正确的。我错过了什么吗?

HTML:

<input type="file" class="form-control" name="documents" (change)="onFileUploadChange($event)">

组件:

onFileUploadChange(_event: any) {
    let file = _event.srcElement.files;
    let postData: any = null; 
    this._fileUploadService.uploadFile(this.uploadURL, file);
}

服务:

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);
    });
}

【问题讨论】:

  • 您有一个内部服务器错误。调试您的服务器端代码以找出问题所在。如果请求有问题,那会告诉你什么。
  • 已解决。这是服务器错误。

标签: javascript ajax angular xmlhttprequest


【解决方案1】:

500 错误表示请求已到达目的地,但目的地出现问题。尝试检查目的地是否有错误。

【讨论】:

    【解决方案2】:

    也许你发送了错误的请求?上次遇到同样的问题,有解决办法:

    Saving Blob as xlsx in Angular2

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      相关资源
      最近更新 更多