2015-09-16 55 views
0

我想通过angularjs(客户端)和Nodejs express(服务器端)上传图片。我不愿意使用表格,因为我的公司没有使用它。Angularjs和Nodejs快递

这是

$scope.uploadPhotoToServer = function() { 
    console.log('will upload to album ' + this.albumName + ' file ' + this.userPhoto); 


    var fd = new FormData(); 
    //Take the first selected file 
    fd.append("file", this.userPhoto); 
    fd.append("album", this.albumName); 

    $http.post('/upload', fd, { 
     withCredentials: true, 
     headers: {'Content-Type': undefined }, 
     transformRequest: angular.identity 
    }); 
}]); 

我想,如果你能向我解释如何工作的,我怎么可能继续提前:)

回答

0

由于我不知道我的控权的组成部分是什么你想知道的或者你不明白的部分,但是这基本上只是创建了一个$ scope成员函数,它将userPhoto和albumName添加到FormData对象(fd)中,该对象表示该表单。然后,该函数将该对象发送给该服务器使用$ http。您可以将$ http视为典型的将数据发送到服务器的ajax命令。 (实际上它使用相同的XMLHttpRequest对象是AJAX确实

如果你需要它,这里是对FORMDATA()参考使用: https://developer.mozilla.org/en-US/docs/Web/API/FormData