Ionic framework 未在服务器ionic3中上载文件
我有一个场景,我正在使用ionic将图像文件从本地驱动器(键入jpeg或png)上载到API端点。下面是我的代码: fileupload.html--> provider.ts--> 我已经解决了CORS问题,同样的问题也没有 当我使用postman发送相同的响应时,它会成功,这就是我在postman中发送的响应Ionic framework 未在服务器ionic3中上载文件,ionic-framework,ionic3,jira-rest-api,ionic-native,ionic-view,Ionic Framework,Ionic3,Jira Rest Api,Ionic Native,Ionic View,我有一个场景,我正在使用ionic将图像文件从本地驱动器(键入jpeg或png)上载到API端点。下面是我的代码: fileupload.html--> provider.ts--> 我已经解决了CORS问题,同样的问题也没有 当我使用postman发送相同的响应时,它会成功,这就是我在postman中发送的响应 Form-data key - "file" (type file) value - "/path/to/my/file" Headers Content-type - appli
Form-data
key - "file" (type file) value - "/path/to/my/file"
Headers
Content-type - application/json
x-attlassian token - no-check
有人能告诉我这里出了什么问题。您必须将内容类型从
application/json
更改为multipart/form data
。您发送的是图像,而不是json文件。最好的方法是将图像编码到base64并发送。一切都取决于服务器需要什么
或者你可以试试这个
const body = file;
const headers = new Headers({'Content-Type': 'image/jpg'});
return this.http.post(this.api+'/issue/'+key+'/attachments, body, {headers: headers}). ...
使用
FormData
上传文件
fileupload.ts
changeListener(event) {
const fd = new FormData();
this.file = event.target.files[0];
fd.append('file', this.file, this.file.name);
this.jira.postAttachment("PM-3",fd)
.subscribe(data => {
console.log(data);
});
}
postAttachment(key, fd): Observable<any> {
const httpOptions = {
headers: new HttpHeaders(
{ 'Content-Type': 'multipart/form-data' },
{ 'Authorization': `Basic ${this.auth.getAuthString()}` })
};
return this.http.post(this.api+'/issue/'+key+'/attachments', fd, httpOptions);
}
provider.ts
changeListener(event) {
const fd = new FormData();
this.file = event.target.files[0];
fd.append('file', this.file, this.file.name);
this.jira.postAttachment("PM-3",fd)
.subscribe(data => {
console.log(data);
});
}
postAttachment(key, fd): Observable<any> {
const httpOptions = {
headers: new HttpHeaders(
{ 'Content-Type': 'multipart/form-data' },
{ 'Authorization': `Basic ${this.auth.getAuthString()}` })
};
return this.http.post(this.api+'/issue/'+key+'/attachments', fd, httpOptions);
}
postAttachment(键,fd):可观察{
常量httpOptions={
标题:新的HttpHeaders(
{“内容类型”:“多部分/表单数据”},
{'Authorization':`Basic${this.auth.getAuthString()}`})
};
返回this.http.post(this.api+'/issue/'+key+'/attachments',fd,httpOptions);
}
关于AngularJS的一个问题,最终的效果是什么(类似的一个方法也可能对您有所帮助):
- 创建一个隐藏的输入反类型文件
- 在changeListener函数中设置它的值
- 之后再从那里发送文件
- 原因是文件输入的某些内置属性让我们将其值识别为file/Blob,而不是许多“复杂”组件使用的路径
- 如前所述,也将其作为多部分文件发送
express
?它不是express JIRA软件是一个在tomcat上运行的Web应用程序
changeListener(event) {
const fd = new FormData();
this.file = event.target.files[0];
fd.append('file', this.file, this.file.name);
this.jira.postAttachment("PM-3",fd)
.subscribe(data => {
console.log(data);
});
}
postAttachment(key, fd): Observable<any> {
const httpOptions = {
headers: new HttpHeaders(
{ 'Content-Type': 'multipart/form-data' },
{ 'Authorization': `Basic ${this.auth.getAuthString()}` })
};
return this.http.post(this.api+'/issue/'+key+'/attachments', fd, httpOptions);
}