在 angular6 中发送表单数据和文件上传显示错误 title1 和 title2 not found
Sending form data along with files upload in angular6 displays error title1 and title2 not found
此代码使用 ng2-file-upload in angular 6 上传文件并且工作正常。现在我想发送表单数据以及 file.To 这种效果,我添加了两个表单输入 title1 和 title2 ,这里是如何实现文件上传。这是我实现它的代码行。所以我更改下面的代码
public uploader: FileUploader = new FileUploader({ url: URL, itemAlias: 'photo' });
到
public uploader: FileUploader = new FileUploader({url: URL, itemAlias: 'photo',
additionalParameter: {
t1: title1, t2: title2 });
但出现错误 src/app/app。component.ts 找不到两个表单输入的 title1 和 title2..
app.component.ts
import { Component, OnInit } from '@angular/core';
import { FileUploader, FileSelectDirective } from 'ng2-file-upload/ng2-file-upload';
const URL = 'http://localhost:3000/api/upload';
import { AppData } from './AppData';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'app';
public uploader: FileUploader = new FileUploader({ url: URL, itemAlias: 'photo' });
/*
public uploader: FileUploader = new FileUploader({url: URL, itemAlias: 'photo',
additionalParameter: {
t1: title1, t2: title2
*/
data = new AppData('', '');
ngOnInit() {
this.uploader.onAfterAddingFile = (file) => { file.withCredentials = false; };
this.uploader.onCompleteItem = (item: any, response: any, status: any, headers: any) => {
console.log('ImageUpload:uploaded:', item, status, response);
alert('File uploaded successfully');
};
}
}
AppData.ts
export class AppData {
constructor(
title1: String,
title2: String
) {}
}
app.component.html
<input type="file" name="photo" ng2FileSelect [uploader]="uploader" />
<input type="text" class="form-control" id="title1"
required
[(ngModel)]="data.title1"/>
<p>Hello {{data.title1}}!</p>
<input type="text" class="form-control" id="title2"
required
[(ngModel)]="data.title2"/>
<p>Hello {{data.title2}}!</p>
<button type="button" class="btn btn-success btn-s"
(click)="uploader.uploadAll()"
[disabled]="!uploader.getNotUploadedItems().length" >
Upload an Image
</button>
问题
您正在尝试访问未定义的变量 title1
和 title2
。
修复
您有 AppData
的实例。您可以直接使用它。
data = new AppData('', '');
.
public uploader: FileUploader = new FileUploader({url: URL, itemAlias: 'photo',
additionalParameter: this.data
Note : do not forget to set the title1
and title2
in data
object.
此代码使用 ng2-file-upload in angular 6 上传文件并且工作正常。现在我想发送表单数据以及 file.To 这种效果,我添加了两个表单输入 title1 和 title2 ,这里是如何实现文件上传。这是我实现它的代码行。所以我更改下面的代码
public uploader: FileUploader = new FileUploader({ url: URL, itemAlias: 'photo' });
到
public uploader: FileUploader = new FileUploader({url: URL, itemAlias: 'photo',
additionalParameter: {
t1: title1, t2: title2 });
但出现错误 src/app/app。component.ts 找不到两个表单输入的 title1 和 title2..
app.component.ts
import { Component, OnInit } from '@angular/core';
import { FileUploader, FileSelectDirective } from 'ng2-file-upload/ng2-file-upload';
const URL = 'http://localhost:3000/api/upload';
import { AppData } from './AppData';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
title = 'app';
public uploader: FileUploader = new FileUploader({ url: URL, itemAlias: 'photo' });
/*
public uploader: FileUploader = new FileUploader({url: URL, itemAlias: 'photo',
additionalParameter: {
t1: title1, t2: title2
*/
data = new AppData('', '');
ngOnInit() {
this.uploader.onAfterAddingFile = (file) => { file.withCredentials = false; };
this.uploader.onCompleteItem = (item: any, response: any, status: any, headers: any) => {
console.log('ImageUpload:uploaded:', item, status, response);
alert('File uploaded successfully');
};
}
}
AppData.ts
export class AppData {
constructor(
title1: String,
title2: String
) {}
}
app.component.html
<input type="file" name="photo" ng2FileSelect [uploader]="uploader" />
<input type="text" class="form-control" id="title1"
required
[(ngModel)]="data.title1"/>
<p>Hello {{data.title1}}!</p>
<input type="text" class="form-control" id="title2"
required
[(ngModel)]="data.title2"/>
<p>Hello {{data.title2}}!</p>
<button type="button" class="btn btn-success btn-s"
(click)="uploader.uploadAll()"
[disabled]="!uploader.getNotUploadedItems().length" >
Upload an Image
</button>
问题
您正在尝试访问未定义的变量 title1
和 title2
。
修复
您有 AppData
的实例。您可以直接使用它。
data = new AppData('', '');
.
public uploader: FileUploader = new FileUploader({url: URL, itemAlias: 'photo',
additionalParameter: this.data
Note : do not forget to set the
title1
andtitle2
indata
object.