angular 上传图片并显示_angular +H5 上传图片 与预览图片

//index.html

司机姓名*

司机身份证号

手机号*

身份证正面

...

上传文件

身份证反面

...

上传文件

驾驶证照片正面

...

上传文件

保存

返回

service.js

var uploadFile = function(dataParam,successFn,errFun){

$http({

method: ‘POST‘,

url: configuration.apiBaseUrl+‘/api/tms/upload/uploadFile‘,

data: dataParam,

headers: {‘Content-Type‘: undefined},

transformRequest: angular.identity

}).success(function (data) {

if(typeof successFn ===‘function‘){

successFn(data);

}

}).error(function (data) {

if(typeof errFun === ‘function‘){

errFun(data);

}

});

};

3:controller.js

function save(frontIdCard,backIdCard,photoDriverCard) {

var fd = new FormData();

if(frontIdCard){

fd.append("frontIdCard", frontIdCard);

}

if(backIdCard){

fd.append("backIdCard", backIdCard);

}

if(photoDriverCard){

fd.append("photoDriverCard", photoDriverCard);

}

myCarService.uploadFile(fd, function (data) {

var fileArr = data.content;

for(var i=0; i < fileArr.length; i++){

if("frontIdCard" === fileArr[i].fileNameKey){

$scope.frontIdCardSrc = fileArr[i].bigImgRtnPath;

}else if("backIdCard" === fileArr[i].fileNameKey){

$scope.backIdCardSrc = fileArr[i].bigImgRtnPath;

}else if("photoDriverCard" === fileArr[i].fileNameKey){

$scope.photoDriverCardSrc = fileArr[i].bigImgRtnPath;

}

}

myDriverService.updateDriver.post({

"driverId":$scope.driverId,

"name":$scope.name,

"idNo":$scope.idNo || "",

"mobile":$scope.mobile,

"frontIdCard":$scope.frontIdCardSrc,

"backIdCard":$scope.backIdCardSrc,

"photoDriverCard":$scope.photoDriverCardSrc,

"partnerNo": AppSession.getPartnerNo()

},function(data){

if(data.code === 0){

messageCenterService.show("操作提示","保存成功", 2000);

$state.go(‘main.myDriver‘, {

}, {

reload: false

});

}else{

messageCenterService.show("操作提示",data.msg, 2000);

}

});

});

}

照片的预览

$scope.previewImage = function(fileId, imgId) {

var preview = document.querySelector("#" + imgId);

var file = document.querySelector("#" + fileId).files[0];

var reader = new FileReader();

reader.onloadend = function () {

preview.src = reader.result;

}

if (file) {

reader.readAsDataURL(file);

} else {

preview.src = "images/noimg.png";

}

};

原文:http://www.cnblogs.com/mamimi/p/7132074.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的示例: 1. 安装依赖 ``` npm install --save @angular/material @angular/cdk @angular/flex-layout ``` 2. 导入模块和组件 在 `app.module.ts` 中导入以下模块和组件: ```typescript import { NgModule } from '@angular/core'; import { BrowserModule } from '@angular/platform-browser'; import { BrowserAnimationsModule } from '@angular/platform-browser/animations'; import { FormsModule } from '@angular/forms'; import { HttpClientModule } from '@angular/common/http'; import { MatInputModule } from '@angular/material/input'; import { MatButtonModule } from '@angular/material/button'; import { MatIconModule } from '@angular/material/icon'; import { MatCardModule } from '@angular/material/card'; import { FlexLayoutModule } from '@angular/flex-layout'; import { AppComponent } from './app.component'; @NgModule({ imports: [ BrowserModule, BrowserAnimationsModule, FormsModule, HttpClientModule, MatInputModule, MatButtonModule, MatIconModule, MatCardModule, FlexLayoutModule ], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule {} ``` 3. 编写组件模板和样式 在 `app.component.html` 中编写组件模板: ```html <div fxLayout="column" fxLayoutAlign="center center"> <mat-card> <mat-card-header> <mat-card-title>上传文件</mat-card-title> </mat-card-header> <mat-card-content> <input type="file" (change)="onFileSelected($event)" /> <mat-card *ngIf="previewUrl"> <mat-card-header> <mat-card-title>预览</mat-card-title> </mat-card-header> <img [src]="previewUrl" /> </mat-card> </mat-card-content> <mat-card-actions> <button mat-raised-button color="primary" (click)="uploadFile()">上传</button> </mat-card-actions> </mat-card> </div> ``` 在 `app.component.css` 中编写组件样式: ```css mat-card { margin-top: 20px; max-width: 500px; } img { max-width: 100%; max-height: 300px; } ``` 4. 编写组件代码 在 `app.component.ts` 中编写组件代码: ```typescript import { Component } from '@angular/core'; import { HttpClient } from '@angular/common/http'; @Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.css'] }) export class AppComponent { file: File | null = null; previewUrl: string | null = null; constructor(private httpClient: HttpClient) {} onFileSelected(event: any) { this.file = event.target.files[0]; this.preview(); } preview() { if (!this.file) { this.previewUrl = null; return; } const reader = new FileReader(); reader.readAsDataURL(this.file); reader.onload = () => { this.previewUrl = reader.result as string; }; } uploadFile() { if (!this.file) { return; } const formData = new FormData(); formData.append('file', this.file); this.httpClient.post('http://example.com/upload', formData).subscribe( response => { console.log(response); }, error => { console.error(error); } ); } } ``` 这个组件包括三个方法: - `onFileSelected`:当用户选择文件时触发,将文件存储到 `file` 变量中,并调用 `preview` 方法预览文件。 - `preview`:使用 `FileReader` 对象读取 `file` 变量中的文件,并将预览结果存储到 `previewUrl` 变量中。 - `uploadFile`:将 `file` 变量中的文件上传到服务器。 需要注意的是,这个示例代码中上传的 URL 地址是假的,需要根据实际情况修改。同时,也需要在后端代码中对文件上传进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值