我正在try 根据一个curl 请求发出一个Angular 请求,这个请求是:
curl -k -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/octet-stream" --data-binary '@/home/linux-user/Desktop/folder/cool-folder/src/assets/img/default-image.png' -X POST https://$ENDPOINT:6060/v1/user/photo\?id\=3591\&image\=996ab426-eb57-4f79-94f4-cfea9aef2cd9 | gunzip | jq
*仅供参考:请求成功,但图像到达时已损坏,而且我正在通过API将图像发送到AWS
ImageService:个
private postHeaders(token: string): HttpHeaders {
return new HttpHeaders({
'Content-Type': 'application/octet-stream',
Authorization: `Bearer ${token}`
});
}
uploadImage(file: File, token: string): Observable<any> {
const headers = this.postHeaders(token);
const formData = new FormData();
formData.append('file', file);
return this.http.post(this.endpoint, formData, { headers, responseType: 'text' })
.pipe(
map((response: any) => JSON.parse(response)),
catchError((error: any) => throwError(error))
);
}
Component个
onFileChange(event: any) {
const file = event.target.files[0];
console.log(file)
if (file) {
this.imageService.uploadImage(file, this.token)
.subscribe((response) => {
console.log(response); // Handle the response data here using 'jq' or other methods.
}, (error) => {
console.error(error);
});
}
}
Another example of the component above个
tried binary
onFileChange(event: any) {
const file = event.target.files[0];
console.log(file)
if (file) {
this.readFileContent(file);
}
}
readFileContent(file: File) {
const reader = new FileReader();
reader.onload = (e: any) => {
const binaryData = e.target.result; // This contains the binary data of the file
this.imageService.uploadImage(new Blob([binaryData]), this.token)
.subscribe((response) => {
console.log(response); // Handle the response data here
}, (error) => {
console.error(error);
});
};
reader.readAsArrayBuffer(file);
}
Html个
<input type="file" (change)="onFileChange($event)">