admin管理员组文章数量:1289957
In my html I have:
name="file" type="file"
accept=".csv"
In ts:
I get file - $event.target.files.item(0);
Then I pass it to the service
uploadCSVFile( file) {
const uploadedFile = new FormData();
uploadedFile.append( 'file', file, file.name);
const url = `MyURL`;
return this.http.post(url, uploadedFile);
}
The problem is that it says File must have a contentType text/csv But when I add headers -
{headers: new HttpHeaders().append('Content-Type', 'text/csv')}
It is plaining that request is not multipart.
In my html I have:
name="file" type="file"
accept=".csv"
In ts:
I get file - $event.target.files.item(0);
Then I pass it to the service
uploadCSVFile( file) {
const uploadedFile = new FormData();
uploadedFile.append( 'file', file, file.name);
const url = `MyURL`;
return this.http.post(url, uploadedFile);
}
The problem is that it says File must have a contentType text/csv But when I add headers -
{headers: new HttpHeaders().append('Content-Type', 'text/csv')}
It is plaining that request is not multipart.
Share Improve this question asked Feb 6, 2020 at 9:55 Victoria UnizhonaVictoria Unizhona 2661 gold badge2 silver badges13 bronze badges 8- Before you explicitly set the headers, is it the server asking for "text/csv", or the Angular http client? I was doing some csv upload stuff this morning and didn't have to set headers on my request, and what you have looks good. My point is, maybe the server method is expecting something different? – Kurt Hamilton Commented Feb 6, 2020 at 10:01
- @KurtHamilton It is the response I receive from the server - File must have a contentType text/csv. Request headers have: Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryh0fSoB1keNQolXCW And when I look on the FormData Sent it has: Content-Type: application/octet-stream – Victoria Unizhona Commented Feb 6, 2020 at 10:04
- Silly question, but it is an actual csv file, not an Excel file with a .csv extension? I assume you see the raw csv content when you open the file in a text editor? – Kurt Hamilton Commented Feb 6, 2020 at 10:06
- @KurtHamilton I have tried so many files - and yes, raw content. Here is an example - support.staffbase./hc/en-us/articles/… – Victoria Unizhona Commented Feb 6, 2020 at 10:09
-
I downloaded one file, and they're semi-colon separated, not ma separated. This is the header:
Username; Identifier;First name;Last name
– Kurt Hamilton Commented Feb 6, 2020 at 10:18
2 Answers
Reset to default 10The issue was related to MIME types mismatch. The files MIME type is not recognized as same across platforms. .csv file in OSX is recognized as text/csv, whereas in Windows it is recognized as application/vnd.ms-excel.
Here is the solution - change line:
uploadedFile.append( 'file', new Blob([file], { type: 'text/csv' }), file.name);
you need to make your content type as multipart you can modify your code as given below
uploadCSVFile( file) {
let formData:FormData = new FormData();
formData.append('uploadFile', file, file.name);
let headers = new Headers();
/** In Angular 5, including the header Content-Type can invalidate your request */
headers.append('Content-Type', 'multipart/form-data');
headers.append('Accept', 'application/json');
let options = new RequestOptions({ headers: headers });
return this.http.post(url, formData, options)
}
another way is to use
headers.append('enctype', 'multipart/form-data');
本文标签: javascriptHow do I upload CSV file via REST API in AngularStack Overflow
版权声明:本文标题:javascript - How do I upload CSV file via REST API in Angular? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741413772a2377387.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论