Angular 角度4。文件上传
我使用Angular 4,我有这个代码Angular 角度4。文件上传,angular,file-upload,Angular,File Upload,我使用Angular 4,我有这个代码 uploadPhoto(file: File):Promise<any> { var formData = new FormData(); formData.append('req', file); return this.http.post(`${this.urlApi}/uploadphoto`, formData, { withCredentials: false, body: formData, })
uploadPhoto(file: File):Promise<any> {
var formData = new FormData();
formData.append('req', file);
return this.http.post(`${this.urlApi}/uploadphoto`, formData, {
withCredentials: false,
body: formData,
})
.toPromise()
.catch(this.handleError); }
我有同样的结果。在chrome的控制台中我看到:内容类型:多部分/表单数据;boundary=----WebKitFormBoundaryCE8MnSwUcCdLLB
当我从邮递员(chrome exstension)发送数据时,我得到了成功的结果。
此代码对我来说非常有效: HTML代码:(这是用于图像)
注意:在方法中,必须为此运算符定义一个局部变量才能在函数中使用它。在imageSrc中,您有一个base64字节的文件数组。那么您的问题是什么?请澄清您的问题。
uploadPhoto2(file:File){
var formData = new FormData();
formData.append('file', file);
var request = new XMLHttpRequest();
request.open('POST', `${this.urlApi}/uploadphoto`);
request.send(formData);
}
<div class="form-group">
<label for="usr">Upload your new photo(Optional):</label> <input type="file" accept=".jpg,.png,.jpeg" (change)="attachFile($event)">
<br>
<img src="{{imageSrc}} | defaultPipe " height="250" weight="250" alt="Image preview..." />
</div>
attachFile(event) : void {
var reader = new FileReader();
let _self = this;
reader.onload = function(e) {
_self.imageSrc = reader.result;
console.log(_self.imageSrc);
};
reader.readAsDataURL(event.target.files[0]);
}