简体   繁体   English

在Angular 4 http请求的Request Payload中,表单数据似乎是其他内容

[英]Form Data Appears to be something else in Request Payload in Angular 4 http request

I am trying to upload a file to the server so what I did was I converted my data json into formData, appended file to it and then sent to a request. 我试图将文件上传到服务器,所以我要做的就是将数据json转换为formData,将文件附加到该文件,然后发送给请求。 The Request Payload that I see in the requesPayload is not JSON but is something else. 我在requesPayload中看到的Request Payload不是JSON,而是其他东西。

Here attached is the request payload screenshot : 以下是请求有效负载屏幕截图:

Here is the component function code : 这是组件功能代码:

doSubmit(data){
 const fd = new FormData();
    fd.append('name','Tirthraj');
    fd.append('file',this.myFile);

    for (var key in data) {
      if (data.hasOwnProperty(key)) {
        fd.append(key,data[key])
        console.log(key + " -> " + data[key]);
      }
    }

    this.myService.doUploadDataWithFile(fd).then();
}

Here is the service function : 这是服务功能:

 doUploadDataWithFile(data): Promise<any> {
    let headers = new Headers();
    // headers.append('Content-Type', 'multipart/form-data');
    headers.append('Content-Type', 'undefined');
    let options = new RequestOptions({ headers: headers });
    console.log("ADDElearning  DATA", data);
    return this.http.post("SERVER_API", data, options).toPromise();
  }

Here is the request payload : 这是请求有效负载:

------WebKitFormBoundary5Ga9J56YQxt16Ey4
Content-Disposition: form-data; name="name"

Tirthraj
------WebKitFormBoundary5Ga9J56YQxt16Ey4
Content-Disposition: form-data; name="file"; filename="524347_335264113233857_1091003137_n.jpg"
Content-Type: image/jpeg


------WebKitFormBoundary5Ga9J56YQxt16Ey4
Content-Disposition: form-data; name="module_title"

312
------WebKitFormBoundary5Ga9J56YQxt16Ey4
Content-Disposition: form-data; name="module_synopsis"

123
------WebKitFormBoundary5Ga9J56YQxt16Ey4
Content-Disposition: form-data; name="estimated_time"

321
------WebKitFormBoundary5Ga9J56YQxt16Ey4
Content-Disposition: form-data; name="module_type"

scorm12
------WebKitFormBoundary5Ga9J56YQxt16Ey4
Content-Disposition: form-data; name="article_title"

123312
------WebKitFormBoundary5Ga9J56YQxt16Ey4
Content-Disposition: form-data; name="article_description"

321
------WebKitFormBoundary5Ga9J56YQxt16Ey4--

I want it to be like JSON Object and not what it is right now. 我希望它像JSON对象,而不是现在的样子。 Could anybody help? 有人可以帮忙吗?

To post JSON representation of <form> , the File object needs to be converted to a data URI ; 要发布<form> JSON表示<form> ,需要将File对象转换为data URI JSON is a string. JSON是一个字符串。

const props = {};
const reader = new FileReader;
reader.onload = () => {
  props["file"] = reader.result;
}

for (let [key, prop] of new FormData(form)) {
  if (!prop instance of File) {
    props[key] = prop;
  } else {
    reader.readAsDataURL(prop);
  }
}

const json = JSON.stringify(props);

As you have mentioned you want to send file along with form data the thing which you have done is absolutely right. 如前所述,您希望将文件与表单数据一起发送,这是绝对正确的。 only thing is missing is correct way to access that file in php. 唯一缺少的是在php中访问该文件的正确方法。 as you are sending form-data. 当您发送表单数据时。 it means you are sending whole form with name of that input field. 这意味着您将使用该输入字段的名称发送整个表单。

so you can access it like 所以你可以像访问它

echo $_POST['module_title'];
var_dump($_FILES['file']);
var_dump($_FILES['file1'])
$filename = $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'],'uploads/'.$filename);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular $ resource服务以表单数据而不是请求有效载荷发送数据 - Angular $resource service send data in Form Data instead of Request Payload 如何将参数传递给 http 请求,该请求以编程方式将 POST 有效负载作为表单数据 - how to pass parameters to an http request that call which takes the POST payload as form-data programmatically 我们可以一次将表单数据全部传递到角度http请求吗 - Can we pass form data to angular http request all at once 如何在角度2的http post请求中发送表单数据? - How to send form data in a http post request of angular 2? 带有自定义数据的Angular HTTP请求 - Angular http request with custom data Http 请求未根据 angular 中的参数获取。参数有问题 - Http request not fetching based on the parameter in angular .There is something wrong with the params 我应该如何在http post请求的请求有效负载中传递json数据 - How should I pass json data in the request payload of http post request 请求有效载荷 - 可选的数据发送 - Request Payload - optional sending of data 在Sencha Touch中请求有效载荷数据 - Request Payload data in Sencha Touch Angular 7 将数据添加到 http 请求的结果 - Angular 7 adding data to result of http request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM