当我尝试上传zip文件时,不幸的是它没有在服务器上上传。它给我错误了一些事情!这是我的代码:
const event = this.fileEvent;
console.log('FileEvent --> ', event);
const fileList: FileList = event.target.files;
if (fileList.length > 0) {
const file: File = fileList[0];
const formData: FormData = new FormData();
formData.append('file', file, file.name);
formData.append('promotion_name' , this.promotionName);
formData.append('template_id' , this.template_id + '' );
const headers = new Headers();
// No need to include Content-Type in Angular 4
headers.append('Content-Type', 'multipart/form-data');
headers.append('Accept', 'application/json');
const options = new RequestOptions({ headers: headers });
this.http.post(this.apiURL + 'api/v1/advertiser/promotion/createPromotion',
这是我的html代码
<div class="form-group row">
<label class="col-md-3 form-control-label" for="file-input">Select File</label>
<div class="col-md-9">
<div class="input-group">
<input id="file-input" class="form-control" name="marker_image" placeholder="Select File" type="file" (change)="fileChange($event)" accept="application/zip,application/ZIP" />
</div>
<div class="col-md-8 file-err-msg">
<span class="help-block err-color" *ngIf="fileupload">Please upload CSV file</span>
</div>
</div>
</div>
这是我的文件选择代码`返回response.Error(c,errors.New(“请提供zip文件”)) }
filenamePrefix := filepath.Join("./assets/promotion/zipfiles/", buildFileName())
// zipFiles name will be stored in filename
filename, err := pickFileName(filenamePrefix)
if err != nil {
return response.Error(c, errors.New("something went wrong please try agian later"))
}`
答案 0 :(得分:0)
您可以使用类似的内容:
<input type="file" formControlName="uploadFile" (change)="uploadFileToServer($event)"/>
import { Component } from '@angular/core';
import { RequestOptions, Headers, Http } from '@angular/http';
@Component({
selector: 'file-uploader',
templateUrl: './uploadFile.component.html',
styleUrls: ['./uploadFile.component.css'],
})
export class FileUploadComponent {
public uploadFileToServer(event) {
let fileList: FileList = event.target.files;
if (fileList.length > 0) {
let file: File = fileList[0];
let formData: FormData = new FormData();
formData.append('uploadFile', file, file.name);
formData.append('fileType', 'zip');
let headers = new Headers();
headers.append('Accept', 'application/json');
let options = new RequestOptions({ headers: headers });
this.http.post('your url here', formData, options)
.map(res => res.json())
.catch(error => Observable.throw(error))
.subscribe(
data => console.log('success'),
error => console.log(error)
)
}
}
}