Skip to content

Instantly share code, notes, and snippets.

View AndreasDickow's full-sized avatar
🎯
Focusing

BIZ Factory GmbH AndreasDickow

🎯
Focusing
View GitHub Profile
@AndreasDickow
AndreasDickow / UploadService.ts
Created August 9, 2017 10:16
Angular2 Image Upload to Django Rest Framework with Form Data
@Injectable()
export class UploadService {
/**
* @param Observable<number>
*/
private progress$: Observable<number>;
/**
* @type {number}
*/
@AndreasDickow
AndreasDickow / settings.component.ts
Created August 9, 2017 10:15
Angular2 Image Upload to Django Rest Framework
let header = this._variableService.getToken();
let input = new FormData();
input.append("username", this.profileForm.controls['username'].value);
input.append("status", this.profileForm.controls['status'].value);
input.append("city", this.profileForm.controls['city'].value);
input.append("want_news", this.profileForm.controls['want_news'].value);
input.append("want_notifications", this.profileForm.controls['want_notifications'].value);
if (this.result !== null) {
@AndreasDickow
AndreasDickow / views.py
Created August 9, 2017 10:08
Ionic2 file Upload to Django Rest Framework
class ProfileImageUploadView(views.APIView):
parser_classes = (FileUploadParser,)
permission_classes = (IsAccountOwner,)
def put(self, request, filename, format=None):
file_obj = request.data['file']
if not request.user.is_anonymous():
profile = request.user.profile
profile.profile_image = file_obj
profile.save()
@AndreasDickow
AndreasDickow / settings.component.ts
Created August 9, 2017 10:07
Ionic2 File Upload to Django Rest Framework
let options: FileUploadOptions = {
fileKey: 'image',
fileName: this.result.substr(this.result.lastIndexOf('/') + 1),
mimeType : 'image/jpeg',
httpMethod : "PUT",
headers: {Authorization: this._variableService.getToken(),}
}
const fileTransfer: FileTransferObject = this.transfer.create();
fileTransfer.upload(this.result, encodeURI(this._variableService.getBaseUrl() + '/api/v2/profile_image/'+this.result.substr(this.result.lastIndexOf('/') + 1)), options)