آپلود فایلها در یک برنامهی Angular به کمک کامپوننت ng2-file-upload
نویسنده: وحید نصیری
تاریخ: ۱۳۹۶/۰۴/۲۲ ۸:۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
>npm install ng2-file-upload --save
>ng g c UploadFile/ng2-file-upload-test
import { FileUploadModule } from "ng2-file-upload";
@NgModule({
imports: [
FileUploadModule
] import { FileUploader, FileUploaderOptions } from "ng2-file-upload";
import { Ticket } from "./../ticket";
export class Ng2FileUploadTestComponent implements OnInit {
fileUploader: FileUploader;
model = new Ticket(); this.fileUploader = new FileUploader(
<FileUploaderOptions>{
url: "api/SimpleUpload/SaveTicket",
headers: [
{ name: "X-XSRF-TOKEN", value: this.getCookie("XSRF-TOKEN") },
{ name: "Accept", value: "application/json" }
],
isHTML5: true,
// allowedMimeType: ["image/jpeg", "image/png", "application/pdf", "application/msword", "application/zip"]
allowedFileType: [
"application",
"image",
"video",
"audio",
"pdf",
"compress",
"doc",
"xls",
"ppt"
],
removeAfterUpload: true,
autoUpload: false,
maxFileSize: 10 * 1024 * 1024
}
); getCookie(name: string): string {
const value = "; " + document.cookie;
const parts = value.split("; " + name + "=");
if (parts.length === 2) {
return decodeURIComponent(parts.pop().split(";").shift());
}
} this.fileUploader.onBuildItemForm = (fileItem, form) => {
for (const key in this.model) {
if (this.model.hasOwnProperty(key)) {
form.append(key, this.model[key]);
}
}
}; this.fileUploader.onCompleteAll = () => {
// clear the form
// this.model = new Ticket();
}; this.fileUploader.onWhenAddingFileFailed = (item, filter, options) => {
// msg: `You can't select ${item.name} file because of the ${filter.name} filter.`
}; this.fileUploader.onErrorItem = (fileItem, response, status, headers) => {
//
}; this.fileUploader.onSuccessItem = (item, response, status, headers) => {
if (response) {
const ticket = JSON.parse(response);
console.log(`ticket:`, ticket);
}
}; submitForm(form: NgForm) {
this.fileUploader.uploadAll();
// NOTE: Upload multiple files in one request -> https://github.com/valor-software/ng2-file-upload/issues/671
} <div class="container">
<h3>Support Form(ng2-file-upload)</h3>
<form #form="ngForm" (submit)="submitForm(form)" novalidate>
<div class="form-group" [class.has-error]="description.invalid && description.touched">
<label class="control-label">Description</label>
<input #description="ngModel" required type="text" class="form-control"
name="description" [(ngModel)]="model.description">
<div *ngIf="description.invalid && description.touched">
<div class="alert alert-danger" *ngIf="description.errors.required">
description is required.
</div>
</div>
</div> <div class="form-group">
<label class="control-label">Screenshot(s)</label>
<input required type="file" multiple ng2FileSelect [uploader]="fileUploader"
class="form-control" name="screenshot">
</div> <div style="margin-bottom: 10px" *ngIf="fileUploader.queue.length">
<h3>Upload queue</h3>
<p>Queue length: {{ fileUploader?.queue?.length }}</p>
<table class="table">
<thead>
<tr>
<th width="50%">Name</th>
<th>Size</th>
<th>Progress</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of fileUploader.queue">
<td><strong>{{ item?.file?.name }}</strong></td>
<td nowrap>{{ item?.file?.size/1024/1024 | number:'.2' }} MB</td>
<td>
<div class="progress" style="margin-bottom: 0;">
<div class="progress-bar" role="progressbar" [ngStyle]="{ 'width': item.progress + '%' }"></div>
</div>
</td>
<td class="text-center">
<span *ngIf="item.isError"><i class="glyphicon glyphicon-remove"></i></span>
</td>
<td nowrap>
<button type="button" class="btn btn-danger btn-xs" (click)="item.remove()">
<span class="glyphicon glyphicon-trash"></span> Remove
</button>
</td>
</tr>
</tbody>
</table>
<div>
<div>
Queue progress:
<div class="progress">
<div class="progress-bar" role="progressbar" [ngStyle]="{ 'width': fileUploader.progress + '%' }"></div>
</div>
</div>
<button type="button" class="btn btn-danger btn-s" (click)="fileUploader.clearQueue()"
[disabled]="!fileUploader.queue.length">
<span class="glyphicon glyphicon-trash"></span> Remove all
</button>
</div>
</div> <div class="progress" style="margin-bottom: 0;">
<div class="progress-bar" role="progressbar" [ngStyle]="{ 'width': item.progress + '%' }"></div>
</div> <button class="btn btn-primary" [disabled]="form.invalid || !fileUploader.queue.length"
type="submit">Submit</button>
</form> System.InvalidOperationException: Incorrect Content-Type: image/jpeg
public async Task<IActionResult> UploadImage(IFormFile file, Guid id)
public class UploadImageViewModel
{
public IFormFile file {get;set;}
public Guid id {get;set;}
}
public async Task<IActionResult> UploadImage(UploadImageViewModel model)
{