Home > Web Front-end > JS Tutorial > Angular Js file upload form-data_javascript technique

Angular Js file upload form-data_javascript technique

WBOY
Release: 2016-05-16 15:41:45
Original
1366 people have browsed it

Foreword: I haven’t updated my blog for a long time. Recently, the company’s PC-side technology selection uses Angular. In the past few days, I have been rushing to put it on the shelves, so I bite the bullet and start using Angular directly. There are many small pits that can be stepped on one after another. Today I encountered a relatively common problem: image uploading.

Theme: The image is uploaded to the server, and then transmitted to Alibaba Cloud through the server.

No more nonsense, just post the front-end code:

$http({
  method: ‘POST‘,
  url: ‘/wechatapp/User/setAvatar‘,
  data: data,
  headers: {
    ‘Content-Type‘: undefined
  },
  transformRequest: function(data) {
    var formData = new FormData();
    formData.append(‘avatar_data‘, data.adata);
    formData.append(‘avatar_file‘, data.file);
    return formData;
  },
  data: {
    adata: scope.avatar_data,
    file: scope.avatar_file
  }
  }).success(function(d) {
    //请求成功
    cb(d);
  }).error(function(err, status) {
    console.log(err);
    cb(err);
  });
Copy after login

In fact, there is no difficulty. The main thing is to cancel the default Content-Type of the post, and then upload it in the form of FormData. Generally, ajax upload files are uploaded in FormData mode.

The above is the article introducing form-data for Angular Js file upload. I hope you like it.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template