【发布时间】:2018-05-03 09:15:19
【问题描述】:
我正在使用 ngx-uploader 模块使用 Angular2+ 上的预签名 url 将图像上传到 s3。
这是我的代码客户端
import { Component, OnInit, Inject, EventEmitter } from '@angular/core';
import { MatDialog, MatDialogRef, MAT_DIALOG_DATA, MatSnackBar } from '@angular/material';
import { UploadOutput, UploadInput, UploadFile, humanizeBytes, UploaderOptions, UploadProgress } from 'ngx-uploader';
@Component({
selector: 'app-upload-dialog',
templateUrl: './upload-dialog.component.html',
styleUrls: ['./upload-dialog.component.scss'],
})
export class UploadDialogComponent implements OnInit {
options: UploaderOptions;
uploadInput: EventEmitter<UploadInput>;
fileName: string;
constructor(
public dialogRef: MatDialogRef<UploadDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: any) {
}
ngOnInit() {
}
uploadFichier(output: UploadOutput): void {
if (output.type === 'addedToQueue' && typeof output.file !== 'undefined') {
this.currentFile = output.file;
this.fileName = output.file.name;
}
// si l'upload est fini
if (output.type === 'done') {
// when upload done
}
}
saveFile() {
const respo = response.json();
let event: UploadInput;
console.log(this.currentFile)
event = {
type: 'uploadFile',
file: this.currentFile,
url: 'url here',
method: 'PUT',
headers: {'Content-Type': this.currentFile.type}
};
this.uploadInput.emit(event);
/** */
}
cancel() {
const cancelEvent: UploadInput = {
type: 'cancelAll'
};
this.uploadInput.emit(cancelEvent);
}
}
<input id="add-scolaire" type="file" accept="image/x-png,image/gif,image/jpeg"
mat-icon-button
ngFileSelect
[options]="options"
(uploadOutput)="uploadFichier($event)"
[uploadInput]="uploadInput">
当我在上传后尝试下载和打开图像时,收到以下消息:“错误解释 JPEG 图像文件(不是 JPEG 文件:以 0x2d 0x2d 开头)”。 可能问题在于,ngx-uploader 上传文件使用 form-data 方法,通过在文件头添加附加信息。
【问题讨论】:
标签: javascript angular file-upload amazon-s3