Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

176
Views
Enviar archivo de Angular a Laravel

Estoy enviando un archivo de imagen de Angular 13.0.4 a mi controlador Laravel 8 , pero Laravel no reconoce este archivo. Es como no enviar nada.

Cuando pruebo Postman o Insomnia funciona perfectamente bien.

Mi controlador Laravel es así:

 public function uploadimage(Request $request) { if ($request->hasFile('image')) { return response()->json(["message" => "A file was sent!"]); } else { return response()->json(["message" => "No file sent!"]); } }

El cartero regresa ¡Se envió un archivo! , retornos angulares blancos ¡No se ha enviado ningún archivo!

Mi Courses.component.ts es así:

 export class CoursesComponent { constructor(private http:HttpClient) { } filedata:any; /* File onchange event */ fileEvent(e:any){ this.filedata = e.target.files[0]; } /* Upload button functioanlity */ onSubmitform(f: NgForm) { var myFormData = new FormData(); const headers = new HttpHeaders(); headers.append('Content-Type', 'multipart/form-data'); headers.append('Accept', 'application/json'); myFormData.append('image', this.filedata); /* Image Post Request */ this.http.post('http://fener.tachyonstudio.com/api/sample-restful-apis', myFormData, { headers: headers }).subscribe(data => { console.log(data); }); }

Y mi Courses.component.html es así:

 <form #f="ngForm" (ngSubmit)="onSubmitform(f)" enctype='multipart/form-data'> <input type="file" name="image" (change)="fileEvent($event)"/> <button>Upload Image</button> </form>

Soy nuevo en Angular, por lo que he estado atrapado aquí durante bastante tiempo.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Intenta usar enctype en lugar de Content-Type

Así que reemplaza tu:

 headers.append('Content-Type', 'multipart/form-data');

por

 headers.append('enctype', 'multipart/form-data');
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!