文件上传适用于邮递员,但不适用于 angular2
file upload works with postman but not with angular2
我对Angular2比较陌生,请考虑。
我在使用 angular 将文件上传到节点服务器时遇到问题 2. 使用邮递员工作正常:
邮递员快照:
enter image description here
但是,当我使用 Angular2 应用程序向服务器发送文件时,它什么也没做:
Angular HTML 组件
<label for="file1">Upload PDF version: </label>
<form class="form-inline">
<div class="form-group">
<input type="file" id="file1" #abc name="fileToUpload" class="form-control" placeholder="Upload PDF">
</div>
<button class="btn btn-primary" type="submit" (click)="onUpload(abc)">Upload</button>
</form>
Angular TS组件:
import { Component, OnInit } from '@angular/core';
import { UploadService } from './upload.service';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-upload',
templateUrl: './upload.component.html',
styleUrls: ['./upload.component.css']
})
export class UploadComponent implements OnInit {
// file1 = document.getElementById("file1");
constructor(private uploadService: UploadService) { }
ngOnInit() {
}
onUpload = (file: File) => {
this.uploadService.uploadfile(file)
.subscribe((res) => {
console.log(res);
},
(err) => {
console.log(err);
});
}
}
服务组件:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class UploadService {
constructor(private http: HttpClient) {
}
uploadfile(file: File): Observable<File> {
console.log(file);
return this.http.post<File>("http://localhost:3000/fileupload", file, { headers: { 'Content-Type': 'application/json' } });
}
}
当然,我一定是漏掉了什么。恳请大家,提供一些解决方案。
我认为你的问题是关于 Headers。
试试这个:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class UploadService {
constructor(private http: HttpClient) {
}
uploadfile(file: File): Observable<File> {
console.log(file);
return this.http.post<File>("http://localhost:3000/fileupload", file, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } });
}
}
如果您查看您的 PostMan 屏幕截图,header 提供的内容完全相同。上传文件的常用方式
您必须创建 formData 才能上传文件。下面是示例代码。
fileDetails: any = {
clientDoc: {}
};
uploadClientDoc(event) {
this.fileDetails.clientMandateForm = event.srcElement.files[0];
this.submitFlag = this.fileDetails.isdaFile ? true : false;
}
submit() {
let formData: FormData = new FormData();
formData.append('clientDoc', this.fileDetails.clientDoc);
var data = this.urService.uploadFiles(formData).subscribe(res => {
//your login
});
}
uploadFiles(data) {
const httpOptions = {
headers: new HttpHeaders({
'Accept': 'application/json',
'Access-Control-Allow-Origin': '*',
'Content-Type': 'multipart/form-data'
})
};
return this.http.post('<url>', data, httpOptions);
}
<div>
<label>Please Select Document</label>
<label class="type-file">+ Upload FIle
<input type="file" accept="application/pdf" id="my_file1" (change)="uploadClientDoc($event)" />
</label>
<label >{{fileDetails.clientDoc.name}}</label>
</div>
<button class="ci-btn float-right-btn" type="button" (click)="submit()" value="Submit">Submit</button>
我对Angular2比较陌生,请考虑。
我在使用 angular 将文件上传到节点服务器时遇到问题 2. 使用邮递员工作正常:
邮递员快照:
enter image description here
但是,当我使用 Angular2 应用程序向服务器发送文件时,它什么也没做:
Angular HTML 组件
<label for="file1">Upload PDF version: </label>
<form class="form-inline">
<div class="form-group">
<input type="file" id="file1" #abc name="fileToUpload" class="form-control" placeholder="Upload PDF">
</div>
<button class="btn btn-primary" type="submit" (click)="onUpload(abc)">Upload</button>
</form>
Angular TS组件:
import { Component, OnInit } from '@angular/core';
import { UploadService } from './upload.service';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-upload',
templateUrl: './upload.component.html',
styleUrls: ['./upload.component.css']
})
export class UploadComponent implements OnInit {
// file1 = document.getElementById("file1");
constructor(private uploadService: UploadService) { }
ngOnInit() {
}
onUpload = (file: File) => {
this.uploadService.uploadfile(file)
.subscribe((res) => {
console.log(res);
},
(err) => {
console.log(err);
});
}
}
服务组件:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class UploadService {
constructor(private http: HttpClient) {
}
uploadfile(file: File): Observable<File> {
console.log(file);
return this.http.post<File>("http://localhost:3000/fileupload", file, { headers: { 'Content-Type': 'application/json' } });
}
}
当然,我一定是漏掉了什么。恳请大家,提供一些解决方案。
我认为你的问题是关于 Headers。
试试这个:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class UploadService {
constructor(private http: HttpClient) {
}
uploadfile(file: File): Observable<File> {
console.log(file);
return this.http.post<File>("http://localhost:3000/fileupload", file, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } });
}
}
如果您查看您的 PostMan 屏幕截图,header 提供的内容完全相同。上传文件的常用方式
您必须创建 formData 才能上传文件。下面是示例代码。
fileDetails: any = {
clientDoc: {}
};
uploadClientDoc(event) {
this.fileDetails.clientMandateForm = event.srcElement.files[0];
this.submitFlag = this.fileDetails.isdaFile ? true : false;
}
submit() {
let formData: FormData = new FormData();
formData.append('clientDoc', this.fileDetails.clientDoc);
var data = this.urService.uploadFiles(formData).subscribe(res => {
//your login
});
}
uploadFiles(data) {
const httpOptions = {
headers: new HttpHeaders({
'Accept': 'application/json',
'Access-Control-Allow-Origin': '*',
'Content-Type': 'multipart/form-data'
})
};
return this.http.post('<url>', data, httpOptions);
}
<div>
<label>Please Select Document</label>
<label class="type-file">+ Upload FIle
<input type="file" accept="application/pdf" id="my_file1" (change)="uploadClientDoc($event)" />
</label>
<label >{{fileDetails.clientDoc.name}}</label>
</div>
<button class="ci-btn float-right-btn" type="button" (click)="submit()" value="Submit">Submit</button>