0

I am using ng2-file-upload to upload my file front end . In html ,

  <input type="file" ng2FileSelect [uploader]="uploader" />

In my ts file , i get the the uploaded file in change event and i have the url ,now i want to know how to post it to the url .

public uploader:FileUploader = new FileUploader({url: URL});

will it post it to the URL specified ? Lets say my URL is http://localhost:8080/test , how can i check my file has reached there successfully ? File Upload In Angular 2? this will do my required job . but i want to give a try with ng2-file-upload. Can someone help me to do it .

Here i am using customized button . so selection of file and upload of file has to happen on click of single button . so is there any option of calling upload method in ts file .Thanks in advance

Community
  • 1
  • 1
ani
  • 446
  • 1
  • 9
  • 31

1 Answers1

0

There's already a working example. http://valor-software.com/ng2-file-upload/

it has TS code & also a working node JS working code.

/*eslint-disable*/
var express = require('express');
var multer = require('multer');
var fs = require('fs');
var app = express();

var DIR = './uploads/';

var upload = multer({dest: DIR});

app.use(function (req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', 'http://valor-software.github.io');
  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.use(multer({
  dest: DIR,
  rename: function (fieldname, filename) {
    return filename + Date.now();
  },
  onFileUploadStart: function (file) {
    console.log(file.originalname + ' is starting ...');
  },
  onFileUploadComplete: function (file) {
    console.log(file.fieldname + ' uploaded to  ' + file.path);
  }
}));

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

app.post('/api', function (req, res) {
  upload(req, res, function (err) {
    if (err) {
      return res.end(err.toString());
    }

    res.end('File is uploaded');
  });
});

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

app.listen(PORT, function () {
  console.log('Working on port ' + PORT);
});

The front end part which uses the fileuploader object which is intialized in the typescript.

in your typescript.

  public uploader:FileUploader = new FileUploader({url: URL});

we use uploader in template & its predefined methods,

<div ng2FileDrop
             [ngClass]="{'nv-file-over': hasBaseDropZoneOver}"
             (fileOver)="fileOverBase($event)"
             [uploader]="uploader"
             class="well my-drop-zone">
            Base drop zone
        </div>

        <div ng2FileDrop
             [ngClass]="{'another-file-over-class': hasAnotherDropZoneOver}"
             (fileOver)="fileOverAnother($event)"
             [uploader]="uploader"
             class="well my-drop-zone">
            Another drop zone
        </div>

        Multiple
        <input type="file" ng2FileSelect [uploader]="uploader" multiple  /><br/>

        Single
        <input type="file" ng2FileSelect [uploader]="uploader" />
    </div>

    <div class="col-md-9" style="margin-bottom: 40px">

        <h3>Upload queue</h3>
        <p>Queue length: {{ uploader?.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 uploader.queue">
                <td><strong>{{ item?.file?.name }}</strong></td>
                <td *ngIf="uploader.isHTML5" nowrap>{{ item?.file?.size/1024/1024 | number:'.2' }} MB</td>
                <td *ngIf="uploader.isHTML5">
                    <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.isSuccess"><i class="glyphicon glyphicon-ok"></i></span>
                    <span *ngIf="item.isCancel"><i class="glyphicon glyphicon-ban-circle"></i></span>
                    <span *ngIf="item.isError"><i class="glyphicon glyphicon-remove"></i></span>
                </td>
                <td nowrap>
                    <button type="button" class="btn btn-success btn-xs"
                            (click)="item.upload()" [disabled]="item.isReady || item.isUploading || item.isSuccess">
                        <span class="glyphicon glyphicon-upload"></span> Upload
                    </button>
                    <button type="button" class="btn btn-warning btn-xs"
                            (click)="item.cancel()" [disabled]="!item.isUploading">
                        <span class="glyphicon glyphicon-ban-circle"></span> Cancel
                    </button>
                    <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" style="">
                    <div class="progress-bar" role="progressbar" [ngStyle]="{ 'width': uploader.progress + '%' }"></div>
                </div>
            </div>
            <button type="button" class="btn btn-success btn-s"
                    (click)="uploader.uploadAll()" [disabled]="!uploader.getNotUploadedItems().length">
                <span class="glyphicon glyphicon-upload"></span> Upload all
            </button>
            <button type="button" class="btn btn-warning btn-s"
                    (click)="uploader.cancelAll()" [disabled]="!uploader.isUploading">
                <span class="glyphicon glyphicon-ban-circle"></span> Cancel all
            </button>
            <button type="button" class="btn btn-danger btn-s"
                    (click)="uploader.clearQueue()" [disabled]="!uploader.queue.length">
                <span class="glyphicon glyphicon-trash"></span> Remove all
            </button>
Parth Ghiya
  • 6,929
  • 2
  • 30
  • 37
  • thanks for your quick reply. As per my understanding it uses multer to upload the file. I want to upload from my Angular TS itself . – ani Mar 03 '17 at 02:58
  • Angular is your front end, not your back end. The file you upload goes to a server or database or external storages , for that u need back end such as node js,php,java – Parth Ghiya Mar 03 '17 at 03:01
  • yeah i understand it requires back end to handle the upload. If you refer the stackoverflow link which i have added in my question they will do post it url , like that i want to know whether this FileUploader = new FileUploader({url: URL}); will do the post thing or do i have to write a post code explicitly as written in link. – ani Mar 03 '17 at 03:05
  • Yes ani, u just need to do that in your ts files, the methods are defined in your markup, I am editing the answer & Highlighting the mark up part, just search for uploader ir markup – Parth Ghiya Mar 03 '17 at 03:53
  • backend is java – ani Mar 03 '17 at 04:06
  • so is ur API ready ? if u just use the above snippet it should work.! Any errors ? – Parth Ghiya Mar 03 '17 at 04:10
  • API is not yet ready.. will get back here if there is any error. Thank you so much for your guidance :) – ani Mar 03 '17 at 05:30
  • Glad to help ...u can upvote and accept as answer if it helped – Parth Ghiya Mar 03 '17 at 06:56
  • it didn't work for me .. In my case I don't need progress and all. As of now I have my file to upload , after passing the URL to fileuploader do I have to call any method in my ts to upload the file . – ani Mar 06 '17 at 17:57
  • I have customized button . so both choose and upload file has to happen from single button itaelf – ani Mar 06 '17 at 18:06
  • it seems like they have used multer package to perform upload .. Can't we do it without any third party – ani Mar 07 '17 at 01:14
  • What u use on your backend...based on your backend you can find code for storing your file ... – Parth Ghiya Mar 07 '17 at 01:17
  • I have to post my file in the URL specified and I have to send the URL where i have placed my file to the java service method . this service method will read the file from the URL provided. So it means the upload has to done from my front end only – ani Mar 07 '17 at 02:24