Problem uploading file with FormData in Angular 6
I'm trying to build a file upload system with Angular 6 for the UI and Lumen for the API.
The file upload works fine when I use the API directly with Postman.
But it's not working with the Angular UI.
I'm using FormData to handle the uploading but the API returns a null value for the file I'm trying to upload.
HTML:
<input type="file" name="document" (change)="onFileChanged($event.target.files)">
Angular code:
public onFileChanged(files: any): void
var file = files[0];
var fd = new FormData();
fd.append('document', file);
this.documentService
.uploadDocument(this.id, fd)
.subscribe(
() =>
console.log('success');
,
() =>
console.log('failure');
);
add a comment |
I'm trying to build a file upload system with Angular 6 for the UI and Lumen for the API.
The file upload works fine when I use the API directly with Postman.
But it's not working with the Angular UI.
I'm using FormData to handle the uploading but the API returns a null value for the file I'm trying to upload.
HTML:
<input type="file" name="document" (change)="onFileChanged($event.target.files)">
Angular code:
public onFileChanged(files: any): void
var file = files[0];
var fd = new FormData();
fd.append('document', file);
this.documentService
.uploadDocument(this.id, fd)
.subscribe(
() =>
console.log('success');
,
() =>
console.log('failure');
);
add a comment |
I'm trying to build a file upload system with Angular 6 for the UI and Lumen for the API.
The file upload works fine when I use the API directly with Postman.
But it's not working with the Angular UI.
I'm using FormData to handle the uploading but the API returns a null value for the file I'm trying to upload.
HTML:
<input type="file" name="document" (change)="onFileChanged($event.target.files)">
Angular code:
public onFileChanged(files: any): void
var file = files[0];
var fd = new FormData();
fd.append('document', file);
this.documentService
.uploadDocument(this.id, fd)
.subscribe(
() =>
console.log('success');
,
() =>
console.log('failure');
);
I'm trying to build a file upload system with Angular 6 for the UI and Lumen for the API.
The file upload works fine when I use the API directly with Postman.
But it's not working with the Angular UI.
I'm using FormData to handle the uploading but the API returns a null value for the file I'm trying to upload.
HTML:
<input type="file" name="document" (change)="onFileChanged($event.target.files)">
Angular code:
public onFileChanged(files: any): void
var file = files[0];
var fd = new FormData();
fd.append('document', file);
this.documentService
.uploadDocument(this.id, fd)
.subscribe(
() =>
console.log('success');
,
() =>
console.log('failure');
);
asked Nov 16 '18 at 2:37
ajgismeajgisme
2011220
2011220
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
I have used form data in my project. have a look in my code.it may help you
uploadFile(data, index)
const formData: FormData = new FormData();
console.log('formData', formData);
formData.append('data', this.tableData.caseId);
formData.append('file', data.filedata);
this.dashService.uploadAttachment(formData, this.encodedData)
.subscribe(
(respData) =>
this.filesArray.splice(index, 1);
,
respError =>
console.log('respError', respError);
);
removeFile(index)
this.filesArray.splice(index, 1);
The below is the service used in the code.
uploadAttachment(data, encodedData)
return this._http.post(this._url + 'cases/attachment', data,
headers:
// 'Content-Type': 'multipart/form-data',
'Accept': 'application/json',
'Authorization': encodedData
);
I was facing problem for the content type i was using in the service. So i have commented it and my code worked fine.
Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh
– ajgisme
Nov 16 '18 at 11:36
add a comment |
OK I found the problem!
It was because I was using an interceptor that was adding JSON headers to every request so it was converting everything to JSON which obviously wouldn't work. So I just removed that header and it worked.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330653%2fproblem-uploading-file-with-formdata-in-angular-6%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I have used form data in my project. have a look in my code.it may help you
uploadFile(data, index)
const formData: FormData = new FormData();
console.log('formData', formData);
formData.append('data', this.tableData.caseId);
formData.append('file', data.filedata);
this.dashService.uploadAttachment(formData, this.encodedData)
.subscribe(
(respData) =>
this.filesArray.splice(index, 1);
,
respError =>
console.log('respError', respError);
);
removeFile(index)
this.filesArray.splice(index, 1);
The below is the service used in the code.
uploadAttachment(data, encodedData)
return this._http.post(this._url + 'cases/attachment', data,
headers:
// 'Content-Type': 'multipart/form-data',
'Accept': 'application/json',
'Authorization': encodedData
);
I was facing problem for the content type i was using in the service. So i have commented it and my code worked fine.
Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh
– ajgisme
Nov 16 '18 at 11:36
add a comment |
I have used form data in my project. have a look in my code.it may help you
uploadFile(data, index)
const formData: FormData = new FormData();
console.log('formData', formData);
formData.append('data', this.tableData.caseId);
formData.append('file', data.filedata);
this.dashService.uploadAttachment(formData, this.encodedData)
.subscribe(
(respData) =>
this.filesArray.splice(index, 1);
,
respError =>
console.log('respError', respError);
);
removeFile(index)
this.filesArray.splice(index, 1);
The below is the service used in the code.
uploadAttachment(data, encodedData)
return this._http.post(this._url + 'cases/attachment', data,
headers:
// 'Content-Type': 'multipart/form-data',
'Accept': 'application/json',
'Authorization': encodedData
);
I was facing problem for the content type i was using in the service. So i have commented it and my code worked fine.
Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh
– ajgisme
Nov 16 '18 at 11:36
add a comment |
I have used form data in my project. have a look in my code.it may help you
uploadFile(data, index)
const formData: FormData = new FormData();
console.log('formData', formData);
formData.append('data', this.tableData.caseId);
formData.append('file', data.filedata);
this.dashService.uploadAttachment(formData, this.encodedData)
.subscribe(
(respData) =>
this.filesArray.splice(index, 1);
,
respError =>
console.log('respError', respError);
);
removeFile(index)
this.filesArray.splice(index, 1);
The below is the service used in the code.
uploadAttachment(data, encodedData)
return this._http.post(this._url + 'cases/attachment', data,
headers:
// 'Content-Type': 'multipart/form-data',
'Accept': 'application/json',
'Authorization': encodedData
);
I was facing problem for the content type i was using in the service. So i have commented it and my code worked fine.
I have used form data in my project. have a look in my code.it may help you
uploadFile(data, index)
const formData: FormData = new FormData();
console.log('formData', formData);
formData.append('data', this.tableData.caseId);
formData.append('file', data.filedata);
this.dashService.uploadAttachment(formData, this.encodedData)
.subscribe(
(respData) =>
this.filesArray.splice(index, 1);
,
respError =>
console.log('respError', respError);
);
removeFile(index)
this.filesArray.splice(index, 1);
The below is the service used in the code.
uploadAttachment(data, encodedData)
return this._http.post(this._url + 'cases/attachment', data,
headers:
// 'Content-Type': 'multipart/form-data',
'Accept': 'application/json',
'Authorization': encodedData
);
I was facing problem for the content type i was using in the service. So i have commented it and my code worked fine.
answered Nov 16 '18 at 5:41
AbhizAbhiz
19014
19014
Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh
– ajgisme
Nov 16 '18 at 11:36
add a comment |
Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh
– ajgisme
Nov 16 '18 at 11:36
Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh
– ajgisme
Nov 16 '18 at 11:36
Thanks - tried that but didn't make a difference, there's not that much actually fundamentally different from our code tbh
– ajgisme
Nov 16 '18 at 11:36
add a comment |
OK I found the problem!
It was because I was using an interceptor that was adding JSON headers to every request so it was converting everything to JSON which obviously wouldn't work. So I just removed that header and it worked.
add a comment |
OK I found the problem!
It was because I was using an interceptor that was adding JSON headers to every request so it was converting everything to JSON which obviously wouldn't work. So I just removed that header and it worked.
add a comment |
OK I found the problem!
It was because I was using an interceptor that was adding JSON headers to every request so it was converting everything to JSON which obviously wouldn't work. So I just removed that header and it worked.
OK I found the problem!
It was because I was using an interceptor that was adding JSON headers to every request so it was converting everything to JSON which obviously wouldn't work. So I just removed that header and it worked.
answered Nov 21 '18 at 11:10
ajgismeajgisme
2011220
2011220
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330653%2fproblem-uploading-file-with-formdata-in-angular-6%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown