用于发送文件的XMLHttpRequest未将数据发送到

2024-06-25 19:29:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从angularjs2应用程序上传文件到python服务器应用程序。

表单数据在发送到服务器之前看起来很完美。包含名称和文件数据。

另一端是等待接受文件数据的pythonapi。但我没有得到数据。调试器PDB被激活,因为发送是由angularjs2应用程序完成的,但我缺少文件数据。

我会做错什么?

在上传.service.ts

import {Injectable} from 'angular2/core';   
import {Http, Headers} from 'angular2/http';

@Injectable()
export class UploadService {

  constructor(private http: Http) { }

  uploader(files: any) {
    let xhr = new XMLHttpRequest();
    const formData = new FormData();
    for(var i = 0; i < files.length; i++){
       formData.append(files[i].name, files[i]);
    }
    var headers = new Headers();
    headers.append('Content-Type', 'multipart/form-data');

    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
          if (xhr.status === 200) {
              resolve(JSON.parse(xhr.response))
          } else {
              reject(xhr.response)
       }
     }
   }
   xhr.open('POST','http://192.168.1.205:5000/zino', true)
   xhr.send(formData)
 }

}

python烧瓶API调试器

如你所见请求值为空:(

^{pr2}$

Tags: 文件数据fromimport应用程序httpnewfiles
1条回答
网友
1楼 · 发布于 2024-06-25 19:29:40

这是RC angular2的一个工作示例

import {Injectable} from '@angular/core';
import {Http, Headers, RequestOptions} from '@angular/http';

@Injectable()
export class UploadService {

  constructor(private http: Http) { }

  uploader(files: any) {
    let xhr = new XMLHttpRequest()
    const formData = new FormData()
    for(var i = 0; i < files.length; i++){
      formData.append(files[i].name, files[i])
    }

    xhr.onreadystatechange = function () {
      if (xhr.readyState === 4) {
          if (xhr.status === 200) {
              //resolve(JSON.parse(xhr.response))
          } else {
              //reject(xhr.response)
        }
      }
   }
   xhr.open('POST','/server/upload-api', true)
   xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest')
   xhr.send(formData)
 }
}

在python端,您可以在

^{pr2}$

相关问题 更多 >