Skip to content
Advertisement

Angular file input, How to get file on php?

I have recived all the infromation by my form, but I can´t get the name or size of the input file, so i can´t use $_FILES by php

HTML – input file

JavaScript

Angular – Add file input to form (formaA : FormGroup)

JavaScript

Angular – Create request

JavaScript

Angular – Send request

JavaScript

Web – Json request

JavaScript

PHP – Try to get the name and info of inout file

JavaScript

Return

JavaScript

Advertisement

Answer

By using reader.readAsDataURL(Fichier1); you transformed the binary file in the browser into a base64 encoded string. That can be verified by looking at the JSON data on the server:

JavaScript

Transforming a base64 encoded string into a binary file and writing it to disk is fairly simple; you can find examples of that here on Stack Overflow. If you want to keep the file names and other info intact, you can get them from the file object in the browser and send them along with your JSON data:

JavaScript

…which will on the server result into something similar to this (I don’t know the PHP JSON parser formatting by heart; JSON typically looks like this):

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement