[英]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.