问题描述
我正在尝试将两种类型的文件发送到已经安装的服务器images
和zip
I am trying to send two type of files in general to server images
and zip
I've already installed
"@ionic-native/file": "^5.16.0",
"@ionic-native/file-transfer": "^5.16.0",
"cordova-plugin-file": "^6.0.2",
"cordova-plugin-file-transfer": "^1.7.1",
我也将文件类导入到组件中,例如:
I also imported files classes into my component like:
从'@ ionic-native/file-transfer/ngx'导入{FileTransfer,FileUploadOptions,FileTransferObject};从'@ ionic-native/file'导入{File};
import { FileTransfer, FileUploadOptions, FileTransferObject } from '@ionic-native/file-transfer/ngx';import { File } from '@ionic-native/file';
构造函数( //等等. 私人转让:FileTransfer, 私人文件:文件 ){}
constructor( // etc. private transfer: FileTransfer, private file: File ) { }
我收到此警告:
这是我查看的表单
<form class="ion-padding" [formGroup]="distributorForm" (ngSubmit)="approveDistributor()">
<ion-row class="ion-padding">
<ion-col size="12">
<ion-input type="file" formControlName="coDoc" placeholder="Company documents"></ion-input>
<small>1- Only <kbd>Zip</kbd> files are allowed. <br> 2- Maximum size: <kbd>10 MB</kbd></small>
</ion-col>
<ion-col size="12">
<ion-button class="ion-margin-top" type="submit" expand="full" color="success" [disabled]="!distributorForm.valid">SEND</ion-button>
</ion-col>
</ion-row>
</form>
这是我当前的组件函数without files
(这会将数据发送到服务.
Here is my current component function without files
(This sending data to service.
approveDistributor() {
const distributorForm = this.distributorForm.value;
// tslint:disable-next-line: max-line-length
this.verifyService.distributorForm(distributorForm.coDoc).subscribe(
data => {
this.alertService.presentToast('Sent successfully.');
},
error => {
this.alertService.presentToast(error['message']);
},
() => {
this.Mainstorage.ready().then(() => {
this.Mainstorage.get('token').then(
data => {
this.alertService.presentToast('Your data is in process after approve you\'ll be able to work with software.');
},
error => {
console.log(error);
}
);
});
}
);
}
这是我的服务功能(它将数据发送到服务器)
Here is my service function (This sends data to server)
distributorForm(
coDoc: String
) {
const headers = new HttpHeaders({
Authorization : this.token.token_type + " " + this.token.access_token,
Accept: 'application/json, text/plain',
'Content-Type': 'application/json'
});
return this.http.post(this.env.companyDocs,
{
coDoc
}, { headers }
);
}
问题
- 我应该担心有关FileTransfer的警告吗?如果可以的话,如何解决?
- 如何获取我的文件正确的路径以将其发送到服务器?
更新
我已经更改了使用formData
的功能,例如:
Update
I have changed my function to use formData
like:
const distributorForm = this.distributorForm.value; //my original line
//added lines
const formData: FormData = new FormData();
formData.append('coDoc', distributorForm);
console.log(formData); // this returns empty array under formData in console.
// tslint:disable-next-line: max-line-length
this.verifyService.distributorForm(formData).subscribe(
...
);
即使使用formData
,我也无法将文件发送到服务器.
even with formData
I cannot send the file to server.
推荐答案
saveComp() {
var value: File = file;//get file from input
this.customService.saveFile(value)
.subscribe((data: any) => {
// do some stuff
}, (error: any) => console.log(error))
return;
}
saveFile(file: File): any {
const formData: FormData = new FormData();
var url="your url";
//example url= "http://localhost:9090/"+ 'api/CrmLead/CreateFollowUpByExcel';
formData.append('FollowUpFile', file, file.name);
return this._http.post(url, formData, this.requestOptions)
.catch(this.errorHandler);
}
requestOptions = {
headers: new HttpHeaders({
'Authorization': 'Bearer ' + token,
})
};
在ApiController C#中
In ApiController C#
var httpRequest = _httpContextAccessor.HttpContext.Request;
var postedFile = httpRequest.Form.Files["FollowUpFile"];
string path = null;
if (postedFile != null)
{
// do some stuff with file
}
这篇关于角度发送文件到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!