GVKun编程网logo

角度10 | Http帖子|字符串数组追加FormData(往字符串数组里面添加元素)

8

在本文中,我们将为您详细介绍角度10|Http帖子|字符串数组追加FormData的相关知识,并且为您解答关于往字符串数组里面添加元素的疑问,此外,我们还会提供一些关于Angular2http.pos

在本文中,我们将为您详细介绍角度10 | Http帖子|字符串数组追加FormData的相关知识,并且为您解答关于往字符串数组里面添加元素的疑问,此外,我们还会提供一些关于Angular 2 http.post FormData()到PHP上传文件、angularjs $http服务 上传 FormData 类型数据、angularjs – 使用$http发送带角度的multipart/form-data文件、c语言中显示字符串数组的长度(数组实现的字符串数组和指针实现的字符串数组)的有用信息。

本文目录一览:

角度10 | Http帖子|字符串数组追加FormData(往字符串数组里面添加元素)

角度10 | Http帖子|字符串数组追加FormData(往字符串数组里面添加元素)

来自this answer:

FormData的append()方法只能接受字符串或blob对象 类型。如果需要附加数组,请使用JSON.stringify()方法 将数组转换为有效的JSON字符串。

src/test/resources
,

statusCode 500是内部服务器错误,它是服务器端的问题。因此,最好检查API是否可以收到您的请求。

FormData的append()方法接受stringblob类型,因此您可以使用JSON.stringify()方法(formData.append('selectedIds',JSON.stringify(selectedIds));)。所以试试这个:

let selectedIds = ["31"];
let sendData = new FormData();
sendData.append('auth',this.dataService.REG_AUTH);
sendData.append('identifier',identifier);
sendData.append('selectedIds',JSON.stringify(selectedIds)); 

this.http.post<any>('APIENDPOINT',sendData).subscribe(data => {
  console.log(data);
},error => {  
  console.log(error);
});

Angular 2 http.post FormData()到PHP上传文件

Angular 2 http.post FormData()到PHP上传文件

我有一个使用A2 CLI的项目,我正在尝试使用带有type =“ file”的输入标签上载文件.就整个工作流程而言,我可以正常运行,但是我无法从客户端到服务器获取文件数据.

我在TypeScript中有以下代码:

myFnc(event){
    let fileList: FileList = event.target.files;
    if(fileList.length > 0) {
        let file: File = fileList[0];
        console.log(file);
        console.log(file.name);
        let formData:FormData = new FormData();
        formData.append('uploadFile', file, file.name);
        let headers = new Headers();
        /*
            this was the culprit:
            headers.append('Content-Type', 'multipart/form-data');
            worked for me by changing it to:
        */
            headers.append('enctype', 'multipart/form-data');
        headers.append('Accept', 'application/json');
        let options = new RequestOptions({ headers: headers });
        console.log(formData);
        console.log(options);
        this.http.post('./assets/data/upload-persona-document.service.PHP', formData, options)
            .map(res => res.json())
            .catch(error => Observable.throw(error))
            .subscribe(
                data =>{
                    console.log(data);
                },
                error =>{
                    console.log(error);
                }
            )
    }
}

我没有任何错误,并在控制台中得到了要检查的结果. (我知道console.log(formData)实际上不会输出formData的内容.我已经尝试过.entries(),但是我已经读到TS中不完全支持FormData方法.)

在upload-persona-document.service.PHP中:

<?PHP
    $target_dir = "../docs/Personas/";
    $json = array();
    $postData = file_get_contents("PHP://input");
    $input = json_decode($postData);
    $json['input'] = $input;
    $json['post'] = ($_POST);
    $json['files'] = $_FILES;
    $target_file = $target_dir . basename($_FILES["uploadFile"]["name"]);
    $uploadOk = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    echo json_encode($json);
    // ...a bunch of code that will act on the data when I can finally get it correctly...

当我收到响应时,我得到:

[Log] {input: null, post: [], files: []} (main.bundle.js, line 1117)

我为丢失的东西感到难过.

解决方法:

因此,经过更多的挖掘和实验之后,结果发现我不得不在上面的headers.append()行中将Content-Type更改为enctype(更新了该位.)

angularjs $http服务 上传 FormData 类型数据

angularjs $http服务 上传 FormData 类型数据

angularjs $http服务 上传 FormData 类型数据

支持版本 1.7.8

angularjs默认请求类型为json,所以需要修改请求类型。

在请求headers,Content-Type返回undefined,让浏览器,自动识别数据类型。

为什么不手动固定类型multipart/form-data?,因为还是会产生数据格式不同的问题,还是自动识别的好。

代码实例

var params = new FormData();
$http.post(url, params, {
  headers: {
    "Content-Type": function () {  // 重点
      return undefined;
    }
  },
  transformRequest: angular.identity // 可选,防止发生意外的数据转换,这样写可以保证数据类型不变
}).then(function (data) {

  // 回调

}).catch((reason) => {
  console.log(reason);
});

angularjs – 使用$http发送带角度的multipart/form-data文件

angularjs – 使用$http发送带角度的multipart/form-data文件

我知道有很多关于这个的问题,但我不能让这个工作:

我想在multipart / form-data中将文件从输入上传到服务器

我尝试了两种方法。第一:

headers: {
  'Content-Type': undefined
},

这导致例如对于图像

Content-Type:image/png

而它应该是multipart / form-data

和另一个:

headers: {
  'Content-Type': multipart/form-data
},

但这要求一个边界标题,我认为不应该手动插入…

什么是解决这个问题的干净方法?
我读过你能做到的

$httpProvider.defaults.headers.post['Content-Type'] = 'multipart/form-data; charset=utf-8';

但我不希望我的所有帖子都是multipart / form-data。默认值应为JSON

看一下FormData对象:
https://developer.mozilla.org/en/docs/Web/API/FormData
this.uploadFiletoUrl = function(file,uploadUrl){
        var fd = new FormData();
        fd.append('file',file);
        $http.post(uploadUrl,fd,{
            transformRequest: angular.identity,headers: {'Content-Type': undefined}
        })
        .success(function(){
        })
        .error(function(){
        });
    }

c语言中显示字符串数组的长度(数组实现的字符串数组和指针实现的字符串数组)

c语言中显示字符串数组的长度(数组实现的字符串数组和指针实现的字符串数组)

c语言中显示字符串数组的长度(数组实现的字符串数组和指针实现的字符串数组)

1、

#include <stdio.h>

int main(void)
{
    char x[][128] = {"aaa","bb","cccccc","d","eee"};
    char *y[] = {"11111","22222222","3333"};
    
    printf("length of x:  %u\n", sizeof(x)/sizeof(x[0]));
    printf("length of y:  %u\n", sizeof(y)/sizeof(y[0]));
    return 0;
}

 

关于角度10 | Http帖子|字符串数组追加FormData往字符串数组里面添加元素的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Angular 2 http.post FormData()到PHP上传文件、angularjs $http服务 上传 FormData 类型数据、angularjs – 使用$http发送带角度的multipart/form-data文件、c语言中显示字符串数组的长度(数组实现的字符串数组和指针实现的字符串数组)的相关信息,请在本站寻找。

本文标签: