【问题标题】:How to get file from Form Data in the server如何从服务器中的表单数据中获取文件
【发布时间】:2020-12-03 20:29:03
【问题描述】:

我无法在服务器上获取文件数据。 _$FILE 说是空的。我通过离子应用程序做这个。我在 http 参数中发送信息。

cargarFoto(idnegocio,formData) {
    let datos = new HttpParams()
    .set("proceso", "subirImgNegocioApp")
    .set("imgRestaurante", formData)
    .set("idnegocio", idnegocio)

    let headerOptions = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded');
    return this.http.post(this.path,datos, {
      observe: 'response',
      responseType: 'text',
      headers: headerOptions
    });
  } 

这是我发送表单数据和其他信息的服务中的代码。 但是当我使用 $_FILES 来获取文件时,它说是空的或空的。我做错了什么?实际上所有的信息都在 $_POST 上。我将帖子返回到应用程序,这就是显示的内容:

正文:“{ "proceso": "subirImgNegocioApp", "imgRestaurante": "[对象 FormData]", “idnegocio”:“4” }"

【问题讨论】:

  • 文件上传请求的 Content-Type 错误。

标签: php sql angular ionic-framework


【解决方案1】:

我可以建议一些不同的方法 - 将所有内容作为表单数据发送:

  sendSomething(someVar: string, files: File[]) {
    const formData = new FormData();
    for (const f of files) {
      formData.append('file[]', f);
    }
    formData.append('someVar', someVar);
    return this.http.post('/someUrl', formData, {
      headers: new HttpHeaders({ enctype: 'multipart/form-data' })
    });
  }

然后在 PHP 中:

$someVar = $_REQUEST['someVar']

文件(或一个文件)将在

$_FILES['file']

【讨论】:

    猜你喜欢
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多