dsgsdg206050 2019-06-11 15:57
浏览 37

Angular PHP使用文件发送名称

Im following some tutorial to upload file on server by php api. I need to send some data with file. Like with file i want to send a name. So it will generate folder by that name and save file in that folder.

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);
?>

I want to send name with file. And in php that name will be the folder of that file. Example All files are now saving in ""uploads"" folder. I need file save in upload>name>file-here. Any one can help in code so please

  • 写回答

1条回答 默认 最新

  • dougaojue8185 2019-06-29 09:16
    关注

    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, photo) {

    const data = new FormData();
    
    data.append('featuredImage', photo);
    
      return this.http.post(url, data,{ headers: header })
      .map(response => response)
      .catch(this.handleError);
    

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 luckysheet
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误