CORS标头不适用于从Angular到PHP的文件上传

问题描述 投票:0回答:1

我正在尝试将图像从Angular 8+前端上传到我的php后端,并且发送文本数据没有问题,但是希望将图像文件发送到我的wamp目录中的文件夹,不幸的是没有雪茄...

[它工作较早,但是今天早晨它决定不再工作了。我尝试添加到CORS标头中,但那里似乎没有错。

HTML:

<input type="button" value="Test" (click)='Advertise($event.target.files)'>

组分:

ToUpload()
  {
    let images = this.carImages.nativeElement;
    let j=10;
    for(let i of images.files)
    { 
      console.log(i);
      if(i.type=='image/jpeg')
      {
        let frmData = new FormData();
        frmData.append('file',i,(j+'.jpg').toString());
        this.uploadService.UploadImages(frmData).subscribe(val=>
        {
        })
      }
      if(i.type=='image/png')
      {
        let frmData = new FormData();
        frmData.append('file',i,(j+'.png').toString());
        this.uploadService.UploadImages(frmData).subscribe(val=>
        {
        })
      }
      j++;
    }

  }
  Advertise(files:FileList)
  {
    this.ToUpload();
  }

服务:

UploadImages(image:FormData):Observable<any>
  {
    return this.httpClient.post(this.apiURL+"/api/BLL/imageUpload.php?action=upload",image) as Observable<any>;
  }

CORS_Headers.php

<?php
// Default Header
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization,Content-Range, Content-Disposition, Content-Description');
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Credentials: true");
header("MIME-Version: 1.0");
header("Content-type:text/html;charset=UTF-8");
// Response type header
header('Content-Type: application/json');
?>

imageUpload.php

<?php
require_once '../BLL/CORS_Headers.php';

//require '../DAL/DBHandler.php';

//use DAL\DBHandler;

$action=$_GET['action'];
if($action=='upload')
{
    $tempPath = $_FILES['file']['tmp_name'];

    // Get File Name
    $actualName = $_FILES['file']['name'];

    // New path
    $actualPath = '../Images/' . $actualName;
    //$tempPath = compressImage($tempPath,$actualPath,60);
    // Move File into new path
    move_uploaded_file($tempPath, $actualPath)
    // Get real path of moved file here
    $realPath = realpath(__DIR__ . '/' . $actualPath);
    // Delete the file
    echo "Uploaded";
}

预期结果:只需上传

实际结果:Access to XMLHttpRequest at 'http://localhost:3000/api/BLL/imageUpload.php?action=upload' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "http://localhost:3000/api/BLL/imageUpload.php?action=upload", ok: false, …}
php angular typescript wamp wampserver
1个回答
0
投票

尝试一下

在.htaccess文件PHP(服务器)端添加以下代码

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header always set Access-Control-Allow-Methods "POST, GET, PUT, DELETE, OPTIONS"
    Header always set Access-Control-Allow-Headers "X-Requested-With, content-type"
</IfModule>

角码

ImageUpload(formData):Observable<any>{
    var token = localStorage.getItem('keyToken');
    const myHeaders = new HttpHeaders({ 'Authorization': token });
    return this.http
    .post(this.Upload_All_Image(), formData,{headers:myHeaders})
    .concatMap(data=>{
      return Observable.of(data);
    }) 
  }
© www.soinside.com 2019 - 2024. All rights reserved.