How to upload File from Angular 7 to Django - get error 403 (Forbidden)
up vote
0
down vote
favorite
I tried now in so many different ways, I can not get a file uploaded with Angular 7 to the Django Backend - shouldn't be so difficult?!
My .html:
<div class="form-group">
<label for="file">Choose File</label>
<input type="file"
id="file"
(change)="handleFileInput($event.target.files)">
</div>
uploader.component.ts
fileToUpload: File = null;
handleFileInput(files: FileList) {
this.fileToUpload = files.item(0);
this.uploadFileToActivity();
}
uploadFileToActivity() {
this.uploaderService.post(this.fileToUpload).subscribe(data => {
// do something, if upload success
}, error => {
console.log(error);
});
}
my uploader.service.ts that also shows the upload progress
public post( fileToUpload: File): Observable<number>{
const url = '/api/upload/';
var subject = new Subject<number>()
const req = new HttpRequest('POST', url, fileToUpload, {
reportProgress: true,
});
this.httpClient.request(req).subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {
const percentDone = Math.round(100 * event.loaded / event.total);
subject.next(percentDone);
} else if (event instanceof HttpResponse) {
subject.complete();
}
});
return subject.asObservable();
}
And in the Django backend:
views.py:
def post(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
#handle_uploaded_file(request.FILES['file'])
return HttpResponseRedirect('/success/url/')
else:
form = UploadFileForm()
return render(request, 'index.html', {'form': form})
forms.py:
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()
and urls.py:
urlpatterns = [
path(r'api/upload/', views.post, name='post'),
]
When I ran this I get
zone.js:2969 POST http://127.0.0.1:8000/api/upload/ 403 (Forbidden)
Do I need to include an authorization token? If yes: how?
Thanks a lot!
EDIT: After the useful input from Martin Urbanec I inspected the file upload request in the Browser. Here the result:
Someone any idea what I need to change in my code above to make this work?
django angular file-upload http-post angular-httpclient
add a comment |
up vote
0
down vote
favorite
I tried now in so many different ways, I can not get a file uploaded with Angular 7 to the Django Backend - shouldn't be so difficult?!
My .html:
<div class="form-group">
<label for="file">Choose File</label>
<input type="file"
id="file"
(change)="handleFileInput($event.target.files)">
</div>
uploader.component.ts
fileToUpload: File = null;
handleFileInput(files: FileList) {
this.fileToUpload = files.item(0);
this.uploadFileToActivity();
}
uploadFileToActivity() {
this.uploaderService.post(this.fileToUpload).subscribe(data => {
// do something, if upload success
}, error => {
console.log(error);
});
}
my uploader.service.ts that also shows the upload progress
public post( fileToUpload: File): Observable<number>{
const url = '/api/upload/';
var subject = new Subject<number>()
const req = new HttpRequest('POST', url, fileToUpload, {
reportProgress: true,
});
this.httpClient.request(req).subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {
const percentDone = Math.round(100 * event.loaded / event.total);
subject.next(percentDone);
} else if (event instanceof HttpResponse) {
subject.complete();
}
});
return subject.asObservable();
}
And in the Django backend:
views.py:
def post(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
#handle_uploaded_file(request.FILES['file'])
return HttpResponseRedirect('/success/url/')
else:
form = UploadFileForm()
return render(request, 'index.html', {'form': form})
forms.py:
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()
and urls.py:
urlpatterns = [
path(r'api/upload/', views.post, name='post'),
]
When I ran this I get
zone.js:2969 POST http://127.0.0.1:8000/api/upload/ 403 (Forbidden)
Do I need to include an authorization token? If yes: how?
Thanks a lot!
EDIT: After the useful input from Martin Urbanec I inspected the file upload request in the Browser. Here the result:
Someone any idea what I need to change in my code above to make this work?
django angular file-upload http-post angular-httpclient
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I tried now in so many different ways, I can not get a file uploaded with Angular 7 to the Django Backend - shouldn't be so difficult?!
My .html:
<div class="form-group">
<label for="file">Choose File</label>
<input type="file"
id="file"
(change)="handleFileInput($event.target.files)">
</div>
uploader.component.ts
fileToUpload: File = null;
handleFileInput(files: FileList) {
this.fileToUpload = files.item(0);
this.uploadFileToActivity();
}
uploadFileToActivity() {
this.uploaderService.post(this.fileToUpload).subscribe(data => {
// do something, if upload success
}, error => {
console.log(error);
});
}
my uploader.service.ts that also shows the upload progress
public post( fileToUpload: File): Observable<number>{
const url = '/api/upload/';
var subject = new Subject<number>()
const req = new HttpRequest('POST', url, fileToUpload, {
reportProgress: true,
});
this.httpClient.request(req).subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {
const percentDone = Math.round(100 * event.loaded / event.total);
subject.next(percentDone);
} else if (event instanceof HttpResponse) {
subject.complete();
}
});
return subject.asObservable();
}
And in the Django backend:
views.py:
def post(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
#handle_uploaded_file(request.FILES['file'])
return HttpResponseRedirect('/success/url/')
else:
form = UploadFileForm()
return render(request, 'index.html', {'form': form})
forms.py:
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()
and urls.py:
urlpatterns = [
path(r'api/upload/', views.post, name='post'),
]
When I ran this I get
zone.js:2969 POST http://127.0.0.1:8000/api/upload/ 403 (Forbidden)
Do I need to include an authorization token? If yes: how?
Thanks a lot!
EDIT: After the useful input from Martin Urbanec I inspected the file upload request in the Browser. Here the result:
Someone any idea what I need to change in my code above to make this work?
django angular file-upload http-post angular-httpclient
I tried now in so many different ways, I can not get a file uploaded with Angular 7 to the Django Backend - shouldn't be so difficult?!
My .html:
<div class="form-group">
<label for="file">Choose File</label>
<input type="file"
id="file"
(change)="handleFileInput($event.target.files)">
</div>
uploader.component.ts
fileToUpload: File = null;
handleFileInput(files: FileList) {
this.fileToUpload = files.item(0);
this.uploadFileToActivity();
}
uploadFileToActivity() {
this.uploaderService.post(this.fileToUpload).subscribe(data => {
// do something, if upload success
}, error => {
console.log(error);
});
}
my uploader.service.ts that also shows the upload progress
public post( fileToUpload: File): Observable<number>{
const url = '/api/upload/';
var subject = new Subject<number>()
const req = new HttpRequest('POST', url, fileToUpload, {
reportProgress: true,
});
this.httpClient.request(req).subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {
const percentDone = Math.round(100 * event.loaded / event.total);
subject.next(percentDone);
} else if (event instanceof HttpResponse) {
subject.complete();
}
});
return subject.asObservable();
}
And in the Django backend:
views.py:
def post(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
#handle_uploaded_file(request.FILES['file'])
return HttpResponseRedirect('/success/url/')
else:
form = UploadFileForm()
return render(request, 'index.html', {'form': form})
forms.py:
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()
and urls.py:
urlpatterns = [
path(r'api/upload/', views.post, name='post'),
]
When I ran this I get
zone.js:2969 POST http://127.0.0.1:8000/api/upload/ 403 (Forbidden)
Do I need to include an authorization token? If yes: how?
Thanks a lot!
EDIT: After the useful input from Martin Urbanec I inspected the file upload request in the Browser. Here the result:
Someone any idea what I need to change in my code above to make this work?
django angular file-upload http-post angular-httpclient
django angular file-upload http-post angular-httpclient
edited Nov 11 at 14:57
asked Nov 10 at 18:34
smaica
117315
117315
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Content-Type
header must be multipart/form-data
to transfer any files. I recommend you to check if this header is sent to your Django backend.
Thanks, I didn't set the content type, unfortunately this didnt solve my problem. Still I get error 403. How can I check if the header is correctly sent? Breakpoints in the backend are never reached.
– smaica
Nov 11 at 9:53
You can open developer console in your browser. For instance in Google Chrome it's F12. In this developer console, find Network tab, open it and try to upload a file. You can then open the request and examine it, including its headers.
– Martin Urbanec
Nov 11 at 13:02
Ah I see, thanks for this idea. Maybe you can help me figuring out what I need to change to make this work? I added my findings above in the question
– smaica
Nov 11 at 14:58
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Content-Type
header must be multipart/form-data
to transfer any files. I recommend you to check if this header is sent to your Django backend.
Thanks, I didn't set the content type, unfortunately this didnt solve my problem. Still I get error 403. How can I check if the header is correctly sent? Breakpoints in the backend are never reached.
– smaica
Nov 11 at 9:53
You can open developer console in your browser. For instance in Google Chrome it's F12. In this developer console, find Network tab, open it and try to upload a file. You can then open the request and examine it, including its headers.
– Martin Urbanec
Nov 11 at 13:02
Ah I see, thanks for this idea. Maybe you can help me figuring out what I need to change to make this work? I added my findings above in the question
– smaica
Nov 11 at 14:58
add a comment |
up vote
0
down vote
Content-Type
header must be multipart/form-data
to transfer any files. I recommend you to check if this header is sent to your Django backend.
Thanks, I didn't set the content type, unfortunately this didnt solve my problem. Still I get error 403. How can I check if the header is correctly sent? Breakpoints in the backend are never reached.
– smaica
Nov 11 at 9:53
You can open developer console in your browser. For instance in Google Chrome it's F12. In this developer console, find Network tab, open it and try to upload a file. You can then open the request and examine it, including its headers.
– Martin Urbanec
Nov 11 at 13:02
Ah I see, thanks for this idea. Maybe you can help me figuring out what I need to change to make this work? I added my findings above in the question
– smaica
Nov 11 at 14:58
add a comment |
up vote
0
down vote
up vote
0
down vote
Content-Type
header must be multipart/form-data
to transfer any files. I recommend you to check if this header is sent to your Django backend.
Content-Type
header must be multipart/form-data
to transfer any files. I recommend you to check if this header is sent to your Django backend.
answered Nov 10 at 23:26
Martin Urbanec
2818
2818
Thanks, I didn't set the content type, unfortunately this didnt solve my problem. Still I get error 403. How can I check if the header is correctly sent? Breakpoints in the backend are never reached.
– smaica
Nov 11 at 9:53
You can open developer console in your browser. For instance in Google Chrome it's F12. In this developer console, find Network tab, open it and try to upload a file. You can then open the request and examine it, including its headers.
– Martin Urbanec
Nov 11 at 13:02
Ah I see, thanks for this idea. Maybe you can help me figuring out what I need to change to make this work? I added my findings above in the question
– smaica
Nov 11 at 14:58
add a comment |
Thanks, I didn't set the content type, unfortunately this didnt solve my problem. Still I get error 403. How can I check if the header is correctly sent? Breakpoints in the backend are never reached.
– smaica
Nov 11 at 9:53
You can open developer console in your browser. For instance in Google Chrome it's F12. In this developer console, find Network tab, open it and try to upload a file. You can then open the request and examine it, including its headers.
– Martin Urbanec
Nov 11 at 13:02
Ah I see, thanks for this idea. Maybe you can help me figuring out what I need to change to make this work? I added my findings above in the question
– smaica
Nov 11 at 14:58
Thanks, I didn't set the content type, unfortunately this didnt solve my problem. Still I get error 403. How can I check if the header is correctly sent? Breakpoints in the backend are never reached.
– smaica
Nov 11 at 9:53
Thanks, I didn't set the content type, unfortunately this didnt solve my problem. Still I get error 403. How can I check if the header is correctly sent? Breakpoints in the backend are never reached.
– smaica
Nov 11 at 9:53
You can open developer console in your browser. For instance in Google Chrome it's F12. In this developer console, find Network tab, open it and try to upload a file. You can then open the request and examine it, including its headers.
– Martin Urbanec
Nov 11 at 13:02
You can open developer console in your browser. For instance in Google Chrome it's F12. In this developer console, find Network tab, open it and try to upload a file. You can then open the request and examine it, including its headers.
– Martin Urbanec
Nov 11 at 13:02
Ah I see, thanks for this idea. Maybe you can help me figuring out what I need to change to make this work? I added my findings above in the question
– smaica
Nov 11 at 14:58
Ah I see, thanks for this idea. Maybe you can help me figuring out what I need to change to make this work? I added my findings above in the question
– smaica
Nov 11 at 14:58
add a comment |
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%2f53242175%2fhow-to-upload-file-from-angular-7-to-django-get-error-403-forbidden%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