2
votes

I am using Angular 2 for the frontend and Node.js for the backend. I am trying to upload a file to Azure storage blob container but keep getting an error when trying to send the image to api. Any help will be appreciated thanks.

This is the error on the server side:

contentType: files.myfile.type,

TypeError: Cannot read property 'type' of undefined

The error on client side

core.js:1521 ERROR 
HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}
error
:
ProgressEvent {isTrusted: true, lengthComputable: false, loaded: 0, total: 0, type: "error", …}
headers
:
HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers: Map(0)}
message
:
"Http failure response for (unknown url): 0 Unknown Error"
name
:
"HttpErrorResponse"
ok
:
false
status
:
0
statusText
:
"Unknown Error"
url
:
null
__proto__
:
HttpResponseBase

upload.component.ts

    export class UploadComponent implements OnInit {
selectedFile: File = null;

  onFileSelected(event) {
this.selectedFile = <File>event.target.files[0];
  }

  onUpload() {
    const fd = new FormData();
    fd.append('image', this.selectedFile, this.selectedFile.name);
    this.http.post('http://localhost:3000/upload', fd , {
    reportProgress : true,
    observe: 'events'
     }).subscribe( event => {
console.log(event);
    });

  }

  constructor(private http: HttpClient) {}

  ngOnInit() {
  }



}

server.js

app.post('/upload', function(req, res) {
    var form = new formidable.IncomingForm();
    form.parse(req, function(err, fields, files) {
        var options = {
                contentType: files.myfile.type,
                metadata: { fileName: files.myfile.name }
        };
        blobSvc.createBlockBlobFromLocalFile('images', files.myfile.name, files.myfile.path, options, function(error, result, response) {
            if(!error){
            // file uploaded
                res.send('file uploaded!');
            }
        });
    });
});
1
can you post how does files JSON look like - Sajeetharan
Hello i am really new to working with JSON. Can you guide me towards how I would do that ? - Charlie Batista
just put console.log(JSON.stringify(files)) and post here - Sajeetharan
all I get is {"isTrusted":true} i put onFileSelected(event) { this.selectedFile = <File>event.target.files[0]; console.log(JSON.stringify(event)); } - Charlie Batista
which means file is not being passed - Sajeetharan

1 Answers

2
votes

As i see the object File which you are passing to the server is null, so it throws the above error.

You could check the issue on the network tab on chrome developer tools and see if your file is being selected. Hope it helps.