Angular PHP使用文件发送名称

时间:2019-06-11 15:57:48

标签: php angular

我正在按照一些教程通过php api将文件上传到服务器上。我需要发送一些带有文件的数据。像文件一样,我想发送一个名称。因此它将通过该名称生成文件夹并将文件保存在该文件夹中。

home.ts

  onFileSelect(event) {
    if (event.target.files.length > 0) {

      const file = event.target.files[0];
      this.form.get('avatar').setValue(file);
    }
  }

  onSubmit() {
  console.log(this.backUrl);
 name = this.backUrl;
  console.log(name);
    const formData = new FormData();
    formData.append('avatar', this.form.get('avatar').value);

    this.uploadService.uploadFile(formData).subscribe(
      (res) => {
        this.uploadResponse = res;
          console.log(res);
      },
      (err) => {  
        console.log(err);
      }
    );
  }

service.ts

  public uploadFile(data,) {
    let uploadURL = 'http://api.igiinsurance.com.pk:8888/file_upload/upload.php';
    return this.httpClient.post<any>(uploadURL, data);
  }

upload.php

<?php 


header('Content-Type: application/json; charset=utf-8');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");

$response = array();
$upload_dir = 'uploads';
$server_url = 'http://api.igiinsurance.com.pk:8888';
if($_FILES['avatar'])
{
    $avatar_name = $_FILES["avatar"]["name"];
    $avatar_tmp_name = $_FILES["avatar"]["tmp_name"];
    $error = $_FILES["avatar"]["error"];
    if($error > 0){
        $response = array(
            "status" => "error",
            "error" => true,
            "message" => "Error uploading the file!"
        );
    }else 
    {
        $random_name = rand(1000,1000000)."-".$avatar_name;
        $upload_name = $upload_dir.strtolower($random_name);
        $upload_name = preg_replace('/\s+/', '-', $upload_name);

        if(move_uploaded_file($avatar_tmp_name , $upload_name)) {
            mkdir("upload/testing3");

            $response = array(
                "status" => "success",
                "error" => false,
                "message" => "File uploaded successfully",
                "url" => $server_url."/".$upload_name
              );
        }else
        {
            $response = array(
                "status" => "error",
                "error" => true,
                "message" => "Error uploading the file!"
            );
        }
    }

}else{
    $response = array(
        "status" => "error",
        "error" => true,
        "message" => "No file was sent!"
    );
}
echo json_encode($response);
?>

我想用文件发送名字。在php中,该名称将是该文件的文件夹。示例现在所有文件都保存在““上载”“文件夹中。我需要将文件保存在upload> name> file-here。任何人都可以提供代码帮助,所以

1 个答案:

答案 0 :(得分:0)

handleInputChange(event:any){     const image = event.target.files [0];

const reader = new FileReader();
if (!image.type.match(/image-*/)) {
  this.Toastr.error('Image are not valid.', 'Failed!');
  return;
} else {
  this.photo = image;

  reader.onload = (event1: ProgressEvent) => {
    this.url = (<FileReader>event1.target).result;
  };
  reader.readAsDataURL(event.target.files[0]);
  this.selectedFile = event.target.files[0];
  this.validateImage = false;

}

} this.ServiceName.saveNewsData(“ api_url”,this.selectedFile).subscribe((response:any)=> {       if(response.status == true){}})

saveNewsData(URL,照片){

const data = new FormData();

data.append('featuredImage', photo);

  return this.http.post(url, data,{ headers: header })
  .map(response => response)
  .catch(this.handleError);

}