简体   繁体   English

Angular js文件上传到php服务器

[英]Angular js file upload to php server

I am developing an e-commerce site where a logged in admin needs to add new a product.我正在开发一个电子商务网站,登录的管理员需要在其中添加新产品。 The product form has product description and product image to upload.产品表格有产品描述和产品图片上传。 I want to catch all form data in an angular controller and pass them to a service to to process.我想捕获 angular controller 中的所有表单数据,并将它们传递给服务进行处理。 So far I have failed.到目前为止我都失败了。

Here is the HTML markup:这是 HTML 标记:

<label for="product_name">Product name</label>
<input id="product_name" type="text" ng-model="addprovm.product.name">
<input id="product_img" type="file" ng-model="file" name="file">

The form is wrapper in an angular function. ng-submit="addprovm.CreateProduct()"该表单是 angular function 中的包装器。ng ng-submit="addprovm.CreateProduct()"

In the my controller I have following:在我的 controller 中,我有以下内容:

function addproductCtrl($http){
    var     addprovm        =   this;               

    // product details.
    addprovm.product = {
        name: "",
        price: "",
        category: "",
        product_img: ""
    };

    
    // submit button function.
    addprovm.CreateProduct  =   function(){         
       console.log(addprovm.product);
    }
}

Here I wanted to console.log the form data and validate (if necessary) before I send them to a service, which then takes this data and passes it to a php script to process.在这里,我想console.log表单数据并在将它们发送到服务之前进行验证(如有必要),然后该服务获取此数据并将其传递给 php 脚本进行处理。

How can I make my php script upload the image file to a location, and insert product info to a mysql db?如何让我的 php 脚本将图像文件上传到某个位置,并将产品信息插入到 mysql 数据库?

This a simple upload file, maybe can help you, return an object with the status of the file.这是一个简单的上传文件,也许可以帮助你,返回一个 object 和文件的状态。 You can add a function to call this file before the INSERT query.您可以在 INSERT 查询之前添加一个 function 来调用此文件。

<?php include_once("../conection.php");?>
<?php
    if(isset($_FILES['file'])){
        $errors = array();        
        $file_name = $_FILES['file']['name'];
        $file_size = $_FILES['file']['size'];
        $file_tmp = $_FILES['file']['tmp_name'];
        $file_type = $_FILES['file']['type'];   
        $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
        $extensions = array("jpg","jpeg","png");

        if(in_array($file_ext, $extensions) === false){
            $errors[]="Is not a valid File.";
        }

        $path = dirname(__FILE__) . '/products/';

        if(file_exists($path.'/'.$file_name)) {
            $errors[] = "This file has already been uploaded.";
        }

        if (!file_exists($path)) {
           mkdir($path, 0777, true);
        }

        if(empty($errors) == true){
            move_uploaded_file($file_tmp, $path.'/'.$file_name);
            echo (json_encode(['file' => $file_name]));
        }else{
            echo(json_encode(['errores' => $errors]));
        }
    }
?>

You can try ng-file-upload ;) and is easy to use您可以尝试ng-file-upload ;) 并且易于使用

$scope.upload = function (file) {
        Upload.upload({
            url: 'upload/url',
            data: {file: file, 'username': $scope.username}
        }).then(function (resp) {
            console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
        }, function (resp) {
            console.log('Error status: ' + resp.status);
        }, function (evt) {
            var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
            console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
        });
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM