【问题标题】:insert product and upload file work separately but not together in Angular 6在 Angular 6 中插入产品和上传文件分开工作但不能一起工作
【发布时间】:2018-07-31 08:38:36
【问题描述】:

我正在开发我的第一个 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;
}

}

还有这项服务

create-product.service.ts

在这个我有createProduct() 方法,它从上面的服务调用uploadFile() 方法和从HTTP 服务调用http.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"]);
            },
            (error)=>
            {
                console.log(error);
            }
        );

        //file is successfully uploaded, then I insert the product:

        this._http.post(`${this.url}/crear-producto`,this.productToInsert,options).pipe(
            map(
                    
                   (res)=>{
    
                       console.log ("res del callback del map" + res);
                       
                       return res.json();
    
                    },(err)=>{
    
                         return err;
    
                    }
                
                
                )
                   
    
    
            ) .subscribe(
    
                (response)=>
                {
                    console.log(response);
                    this._router.navigate(['all-products']);
                  
    
                },
                (error)=>
                {
                     console.log(error);

                }
    
            );



}

}

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

为此,我需要捕获响应,然后使用 result["complete_name"] 更改对象 productToInsert.imagen

为此,我仅在承诺响应成功时才使用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 响应,但在这种情况下,对象似乎是空的(所有字段都为空),而不是来自表单的对象。

这是使用的表格

create-product-component.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>

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

  • 避免服务,直接放在component.ts中

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

  • 绑定“this”作用域,看看箭头函数内部的作用域是否有问题。

但是没有任何效果。不知道为什么单独使用对象和这两种方法可以访问和正确使用,但是一起使用时发现这个问题,这是老师告诉我们的。

可能对您有用的提示是,我在某处遇到错误(其中包括大声笑),但我必须说这个错误,其他人给我上传文件并以单独的方式创建产品没有问题,所以我计划稍后调试它们。

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

【问题讨论】:

    标签: angular typescript angular6


    【解决方案1】:

    我解决了。这是一个虚拟的东西:

    问题是,在我的组件中,来自以前的版本,我在表单上遇到了:

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

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

    【讨论】:

      猜你喜欢
      • 2014-02-06
      • 2017-03-15
      • 2020-12-13
      • 1970-01-01
      • 2021-08-05
      • 2017-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多