how to upload File in angularjs frontend

一世执手 提交于 2020-01-07 07:13:23

问题


I want to Upload a data api with the following signature but I want to double check how the data is received.I have used just ordinary modules in angular to upload the file but I want to check how the file pushed to the api. I want the file to be collection of bytes as it reaches the api but here am uploading the just the file. Does the internal transfer protocol change it to bytes ?

notice the file has type of collection of bytes. How should I upload that


回答1:


For this I use the ng-file-upload. Using the Upload service to call your api like this:

Upload.upload({
  url: '/api/uploadFile',
  fields: {fileName: 'fileName', fileExt: '.doc'},
  file: file
})

The file will be uploaded as type ArrayBuffer and you can do what you need to on the back end.

Here is a snippet for the download using FileSaver,js:

$http.post('/api/downloadFile', 'fileName', {responseType: "arraybuffer"}).
  success(function(data) {
    var blob = new Blob([data], { type: '.doc' });
    saveAs(blob, file.fileName);
  })


来源:https://stackoverflow.com/questions/31589856/how-to-upload-file-in-angularjs-frontend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!