Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 插入产品和上传文件分别工作,但不在一起_Angular_Typescript_Angular6 - Fatal编程技术网

Angular 插入产品和上传文件分别工作,但不在一起

Angular 插入产品和上传文件分别工作,但不在一起,angular,typescript,angular6,Angular,Typescript,Angular6,我正在开发我的第一个CRUD测试应用程序 我有一个典型的产品名称,价格表。。。以及用于上载产品的输入文件 我为表单的更改事件创建了一个事件处理程序方法。很好 我创建了这个uploadFile()方法,效果很好 上传文件服务.ts import {Injectable, Input} from '@angular/core'; import {Http, Response, Headers, RequestOptions} from '@angular/http'; import {Router,

我正在开发我的第一个CRUD测试应用程序

我有一个典型的产品名称,价格表。。。以及用于上载产品的输入文件

我为表单的更改事件创建了一个事件处理程序方法。很好

我创建了这个uploadFile()方法,效果很好

上传文件服务.ts

import {Injectable, Input} from '@angular/core';
import {Http, Response, Headers, RequestOptions} from '@angular/http';
import {Router, ActivatedRoute,Params} from '@angular/router';

import {map} from 'rxjs/operators';
import {_GLOBAL} from './global.service';
import {Observable} from 'rxjs';
import {Producto} from '../models/Producto.model';


 @Injectable()

export class UploadFileService
{
    public url:string;
    public filesToUpload:Array<File>;

    constructor()
    {
        this.url=_GLOBAL.url;//this has the URL of the REST service
        this.filesToUpload=[];
    }


    uploadFile(url:string, params:Array<string>,filesToUpload:Array<File>)
    {
        return new Promise((resolve, reject)=>{

        var formData:any= new FormData();

        var asyncRequest=new XMLHttpRequest();

        for(var i=0; i<filesToUpload.length;++i)
        {
             formData.append('filesUploaded[]',filesToUpload[i],filesToUpload[i].name);
        }

        asyncRequest.onreadystatechange=function(){
            if(asyncRequest.readyState==4){
                if(asyncRequest.status==200){
                    resolve(JSON.parse(asyncRequest.response));
                }else{
                    reject(asyncRequest.response);
                }
            }
        }

        asyncRequest.open('POST',url,true);

        asyncRequest.send(formData);
   });
}



fileChangeEvent(ElementObjectReferenceWhichTriggersEvent:any)// in this case, the input type="file"
{
    this.filesToUpload=<Array<File>>ElementObjectReferenceWhichTriggersEvent.target.files;//captura los archivos mandados en el input

    console.log(ElementObjectReferenceWhichTriggersEvent.target);

    console.log(ElementObjectReferenceWhichTriggersEvent.target.files[0]);

    console.log(this.filesToUpload);

   // return this.filesToUpload;
}
}

它的工作原理是:上传文件并正确插入产品。。。问题是,在数据库上,我希望
结果[“complete_name”]
存储在图像字段中,然后我可以在以后获得所有产品时显示图像,而不是
http.post()包含在字段中的“c:/fakepath/PICTURE.PNG”

为此,我需要捕获响应,然后使用
结果[“complete\u name”]

为此,我仅在promise响应成功时才使用
http.post()
。因此,当上传图像时,我捕获响应结果,将其添加到post()方法上作为参数传递的对象中,然后发送它

import {Injectable} from '@angular/core';
import {Http, Response, Headers, RequestOptions} from '@angular/http';
import {Router, ActivatedRoute,Params} from '@angular/router';

import {map} from 'rxjs/operators';
import {_GLOBAL} from './global.service';
import {Observable} from 'rxjs';
import {Producto} from '../models/Producto.model';
import {UploadFileService} from './upload-file.service';


@Injectable()

export class CreateProductService{

    public url:string;
    public errorMessage:string;
    public productToInsert:Producto;
    public imageData:string;

    constructor(
                    private _http:Http,
                    private _route:ActivatedRoute, 
                    private _router:Router,
                    private _uploadFile:UploadFileService
                )
    {
   
   
        this.url=_GLOBAL.url;
        this.errorMessage="";
  
        this.productToInsert=new Producto("","","","");
  
    }//end constructor


    ngOnInit()
    {


    }

 
    createProduct()
    {
    
    

        let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });

        let options = new RequestOptions({ headers: headers });

        this._uploadFile.uploadFile(`${this.url}/upload-file`,[],this._uploadFile.filesToUpload).then(

        (result)=>{
            console.log(result["complete_name"]);
            this.productToInsert.imagen=result["complete_name"];


            this._http.post(`${this.url}/crear-producto`,this.productToInsert,options).pipe(
        
        
                map(
                    
                    (res)=>{
                               return res.json();
    
                    },(err)=>{
    
                               return err;
    
                    }
                
                
                )).subscribe(

                    (response)=>
                    {
                        console.log( response);
                        this._router.navigate(['all-products']);
                  
    
                    },
                    (error)=>
                    {
                        console.log(+error);
                   
                     }
    
                );

            },
            (error)=>{
                console.log(error);

            }

       );
   }

}
但这是行不通的。我得到了一个200响应,但是这个例子中的对象似乎是空的(所有字段都为null),而不是来自表单的

这是所用的表格

创建产品组件.html

<form #formCreateProduct="ngForm" (ngSubmit)="_createProduct.createProduct();  formCreateProduct.reset()" class="col-lg-6" id="form-crear-producto-id">

    <div class="form-group">

        <label for="nombreLabel"> Name of the new product:
   
            <span *ngIf="!nombre.valid && nombre.touched && _createProduct.productToInsert.nombre.length != 0" class="label label-danger">Minimun 3 characters please</span>

        </label>  

  
       <input type="text" class="form-control" name="name" #nombre="ngModel" pattern =".{3,}"[(ngModel)]="_createProduct.productToInsert.nombre" required /> <br/>



    </div>

    <div class="form-group">

        <label for="priceLabel">Price ( € ): (please decimals with a dot. Ex:29.5)

            <span *ngIf="!precio.valid && precio.touched && _createProduct.productToInsert.precio.length != 0" class="label label-danger">Price is not valid. At least one number please, and only numbers</span>

       </label>

       <input type="text"  class="form-control" name="price" #precio="ngModel" pattern="[0-9.]+" [(ngModel)]="_createProduct.productToInsert.precio" required />  <br/>


    </div>


    <div class="form-group">

        <label for="imageLabel">Image:</label>

         <!--file doesnt suport the ngmodel-->
            <input type="file"  class="form-control" name="imagen" (change)="_uploadFile.fileChangeEvent($event)" [(ngModel)]="_createProduct.productToInsert.imagen" required /> <br/>

    </div>


    <div class="form-group">

        <label for="descriptionLabel">Description:</label>

        <div [ngClass]="{'TopLength': _createProduct.productToInsert.descripcion.length==300}">{{_createProduct.productToInsert.descripcion.length}}/300</div>

           <textarea name="description" class="form-control" maxlength="300" #descripcion="ngModel" [(ngModel)]="_createProduct.productToInsert.descripcion" cols="40" rows="3" ></textarea> <br/>


     </div>
  
  
  
     <input type="submit" value={{title}}  [disabled]="formCreateProduct.invalid" class ="btn btn-lg btn-success" /> <br/>

</form>

新产品名称:
最少3个字符

价格(欧元):(请用小数点表示。例如:29.5) 价格无效。请至少输入一个数字,并且只输入数字
图片:
说明: {{{u createProduct.productToInsert.Description.length}/300

我知道,因为这是我第一个使用angular的应用程序,服务和类似的东西使用起来可能有点奇怪,但我尝试了:

  • 避免使用服务,并将其直接放在component.ts中

  • 只为create-component-service.ts使用一个服务,将所有方法(uploadFile、eventHandler、createProduct)放在那里,并在组件上使用它们(我想这实际上是使用服务的正确方式)

  • 绑定“this”作用域,查看arrow函数中的作用域是否存在任何问题

但什么都不管用。我不知道为什么当我单独使用对象和这两种方法时,我可以访问并正确使用它们,但是当我同时使用这两种方法时,我发现了这个问题,这就是老师告诉我们要做的

一个可能对您有用的提示是,在某个地方我遇到了一个错误(其中包括lol),但我必须说,这个错误和其他错误让我可以以单独的方式上传文件和createproduct,所以我计划稍后调试它们

Uncaught (in promise): SecurityError: The operation is insecure. ./node_modules/@angular/platform-browser/fesm5/platform-browser.js/DefaultDomRenderer2.prototype.setProperty@http://localhost:4200/vendor.js:56793:9 ./node_modules/@angular/core/fesm5/core.js/DebugRenderer2.prototype.setProperty@http://localhost:4200/vendor.js:42592:9 ./node_modules/@angular/forms/fesm5/forms.js/DefaultValueAccessor.prototype.writeValue@http://localhost:4200/vendor.js:48231:9 setUpModelChangePipeline/<@http://localhost:4200/vendor.js:49205:9 ./node_modules/@angular/forms/fesm5/forms.js/FormControl.prototype.setValue/<@http://localhost:4200/vendor.js:50262:65 ./node_modules/@angular/forms/fesm5/forms.js/FormControl.prototype.setValue@http://localhost:4200/vendor.js:50262:13 ./node_modules/@angular/forms/fesm5/forms.js/NgModel.prototype._updateValue/<@http://localhost:4200/vendor.js:51617:46 ./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invoke@http://localhost:4200/polyfills.js:2710:17 onInvoke@http://localhost:4200/vendor.js:35055:24 ./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invoke@http://localhost:4200/polyfills.js:2709:17 ./node_modules/zone.js/dist/zone.js/</Zone.prototype.run@http://localhost:4200/polyfills.js:2460:24 scheduleResolveOrReject/<@http://localhost:4200/polyfills.js:3194:29 ./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invokeTask@http://localhost:4200/polyfills.js:2743:17 onInvokeTask@http://localhost:4200/vendor.js:35046:24 ./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invokeTask@http://localhost:4200/polyfills.js:2742:17 ./node_modules/zone.js/dist/zone.js/</Zone.prototype.runTask@http://localhost:4200/polyfills.js:2510:28 drainMicroTaskQueue@http://localhost:4200/polyfills.js:2917:25 ./node_modules/zone.js/dist/zone.js/</ZoneTask.invokeTask@http://localhost:4200/polyfills.js:2822:21 invokeTask@http://localhost:4200/polyfills.js:3862:9 globalZoneAwareCallback@http://localhost:4200/polyfills.js:3888:17

Uncaught(承诺中):SecurityError:操作不安全/node_modules/@angular/platform browser/fesm5/platform-browser.js/defaultdomrender2.prototype。setProperty@http://localhost:4200/vendor.js:56793:9./node_modules/@angular/core/fesm5/core.js/debugrender2.prototype。setProperty@http://localhost:4200/vendor.js:42592:9./node_modules/@angular/forms/fesm5/forms.js/DefaultValueAccessor.prototype。writeValue@http://localhost:4200/vendor.js:48231:9 setUpModelChangePipeline/我解决了它。这是一件愚蠢的事情:

问题是,在我的组件中,从以前的版本来看,我在表单上:


reset()方法在我实际发送表单值之前将所有表单值设置为null。删除了这个方法(在新版本中,我做了一个重定向,所以我不需要它),问题就解决了。这两天…:O

Uncaught (in promise): SecurityError: The operation is insecure. ./node_modules/@angular/platform-browser/fesm5/platform-browser.js/DefaultDomRenderer2.prototype.setProperty@http://localhost:4200/vendor.js:56793:9 ./node_modules/@angular/core/fesm5/core.js/DebugRenderer2.prototype.setProperty@http://localhost:4200/vendor.js:42592:9 ./node_modules/@angular/forms/fesm5/forms.js/DefaultValueAccessor.prototype.writeValue@http://localhost:4200/vendor.js:48231:9 setUpModelChangePipeline/<@http://localhost:4200/vendor.js:49205:9 ./node_modules/@angular/forms/fesm5/forms.js/FormControl.prototype.setValue/<@http://localhost:4200/vendor.js:50262:65 ./node_modules/@angular/forms/fesm5/forms.js/FormControl.prototype.setValue@http://localhost:4200/vendor.js:50262:13 ./node_modules/@angular/forms/fesm5/forms.js/NgModel.prototype._updateValue/<@http://localhost:4200/vendor.js:51617:46 ./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invoke@http://localhost:4200/polyfills.js:2710:17 onInvoke@http://localhost:4200/vendor.js:35055:24 ./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invoke@http://localhost:4200/polyfills.js:2709:17 ./node_modules/zone.js/dist/zone.js/</Zone.prototype.run@http://localhost:4200/polyfills.js:2460:24 scheduleResolveOrReject/<@http://localhost:4200/polyfills.js:3194:29 ./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invokeTask@http://localhost:4200/polyfills.js:2743:17 onInvokeTask@http://localhost:4200/vendor.js:35046:24 ./node_modules/zone.js/dist/zone.js/</ZoneDelegate.prototype.invokeTask@http://localhost:4200/polyfills.js:2742:17 ./node_modules/zone.js/dist/zone.js/</Zone.prototype.runTask@http://localhost:4200/polyfills.js:2510:28 drainMicroTaskQueue@http://localhost:4200/polyfills.js:2917:25 ./node_modules/zone.js/dist/zone.js/</ZoneTask.invokeTask@http://localhost:4200/polyfills.js:2822:21 invokeTask@http://localhost:4200/polyfills.js:3862:9 globalZoneAwareCallback@http://localhost:4200/polyfills.js:3888:17