简体   繁体   English

无法绑定到“上载器”,因为它不是“输入”的已知属性

[英]Can't bind to 'uploader' since it isn't a known property of 'input'

i was new in ionic and angular i want small project for testing in ionic that upload image by post method and nodejs express file images.... 我是离子和角度的新手,我希望通过离子方法测试小型项目,该项目通过后期方法上传图像,nodejs表示文件图像。

but when i run displayed error for [uploader] in my input... 但是当我在输入中运行[uploader]时显示错误...

there is my code of home.html 这是我的home.html代码

 <ion-header>
  <ion-toolbar>
    <ion-buttons slot="start">
      <ion-menu-button></ion-menu-button>
    </ion-buttons>
    <ion-title>
      Home
    </ion-title>
  </ion-toolbar>
</ion-header>

<ion-content>

  <div class="container">
    <input type="file" name="photo" ng2FileSelect [uploader]="uploader" />
    <button type="button" class="btn btn-success btn-s" 
      (click)="uploader.uploadAll()" 
      [disabled]="!uploader.getNotUploadedItems().length" >
          Upload an Image
    </button>
  </div>

    <ion-card>
      <ion-card-header>
        <img src="http://localhost:3000/images/home.jpg" alt="">
      </ion-card-header>
    </ion-card>
</ion-content>

and this my home.ts 这是我的home.ts

import { Component } from '@angular/core';
import { HttpClient, HttpEventType } from '@angular/common/http';
// import {DomSanitizer} from '@angular/platform-browser';
import {  FileUploader, FileSelectDirective } from 'ng2-file-upload';
const URL = 'http://localhost:3000/images/';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {
  title = 'ng8fileupload';
  public uploader: FileUploader = new FileUploader({ url: URL, 
  itemAlias: 'photo' });


  public db: any;
  public image: any;
  base64Image;
  fileData: File = null;
  constructor(private http: HttpClient,) {

  }
  ngOnInit() {
    this.uploader.onAfterAddingFile = (file) => { file.withCredentials 
    = false; };
    this.uploader.onCompleteItem = (item: any, response: any, status: 
    any, headers: any) => {
         console.log('ImageUpload:uploaded:', item, status, response);
         alert('File uploaded successfully');
    };
 }
}

and this is my app.module.ts 这是我的app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { RouteReuseStrategy } from '@angular/router';

import { IonicModule, IonicRouteStrategy } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';
import { FileSelectDirective, FileUploadModule } from 'ng2-file- 
upload';
import { FormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { AppRoutingModule } from './app-routing.module';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
  declarations: [
    AppComponent
  ],
  entryComponents: [],
  imports: [
    BrowserModule,
    HttpClientModule,
    FileUploadModule,
    FormsModule,
    IonicModule.forRoot(),
    AppRoutingModule,

  ],
  providers: [
    StatusBar,
    SplashScreen,
    { provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
  ],
  bootstrap: [AppComponent]
})
export class AppModule {}

and this serverside 和这个服务器端

const path = require('path');
const express = require('express');
const multer = require('multer');
const bodyParser = require('body-parser')
const app = express();

const DIR = '/images';

let storage = multer.diskStorage({
    destination: (req, file, cb) => {
      cb(null, DIR);
    },
    filename: (req, file, cb) => {
      cb(null, file.fieldname + '-' + Date.now() + '.' + path.extname(file.originalname));
    }
});
let upload = multer({storage: storage});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

app.use(function (req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', 'http://localhost:8100');
  res.setHeader('Access-Control-Allow-Methods', 'POST');
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested- 
   With,content-type');
  res.setHeader('Access-Control-Allow-Credentials', true);
  next();
});

app.get('/api', function (req, res) {
  res.end('file catcher example');
});

app.post('/api/upload',upload.single('photo'), function (req, res) {
    if (!req.file) {
        console.log("No file received");
        return res.send({
          success: false
        });

      } else {
        console.log('file received');
        return res.send({
          success: true
        })
      }
});

const PORT = process.env.PORT || 3000;

app.listen(PORT, function () {
  console.log('Node.js server is running on port ' + PORT);
});

please help me 请帮我

Inside your home.ts file component decorator add the directive for the file Uploader package you have used. 在home.ts文件组件装饰器中,添加您已使用的文件Uploader包的指令。 It is shown below: 如下图所示:

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
  directives: [FILE_UPLOAD_DIRECTIVES]
})

Also change the import line as mentioned below: 还要如下所述更改导入行:

import {  FileUploader, FILE_UPLOAD_DIRECTIVES } from 'ng2-file-upload';

Also change the import line in app.module.ts as well! 还要在app.module.ts中更改导入行!

I am sure this should work! 我相信这应该可行!

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

相关问题 “无法绑定到‘ngModel’,因为它不是‘输入’的已知属性” - “Can't bind to 'ngModel' since it isn't a known property of 'input' ” Angular 4:无法绑定到&#39;ngModel&#39;,因为它不是&#39;input&#39;的已知属性 - Angular 4: Can't bind to 'ngModel' since it isn't a known property of 'input' 无法绑定到“ formGroup”,因为它不是“ form”的已知属性。 (“ <div class=”upload-right“><div class=”signup"> - Can't bind to 'formGroup' since it isn't a known property of 'form'. (“ <div class=”upload-right“> <div class=”signup"> 无法在Fine Uploader中删除文件 - Can't delete file in Fine uploader 如何将付款从一个用户发送到另一个在付款创建时未知的用户? - How can I send payments from one user to another who isn't known at the time of payment creation? 无法在 nodejs 中绑定参数 - Can't bind params in nodejs 无法改变这个属性 - Can't change property of this Typescript - 我的属性装饰器不起作用,为什么? - Typescript - My property decorator isn't working, why? 检查mongodb文档是否具有架构中未定义的属性? - Check if mongodb document has a property that isn't defined in schema? 反应 | 为什么输入的 defaultValue 没有更新? - React | Why the defaultValue of the input isn't being updated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM