简体   繁体   English

如何将请求从动作表单发送到angularjs

[英]how to send request from action form to angularjs

im using this code to upload file to my REST API, and it works fine , this is my form : 即时通讯使用此代码将文件上传到我的REST API,并且工作正常,这是我的表单:

<form action="webresources/documents/upload" 
      method="post" 
      enctype="multipart/form-data">

    <p>
        Select a file : 
        <input type="file" name="file" size="45" />
    </p>
    <input type="submit" value="Upload" />

</form>

and this is my REST API : 这是我的REST API:

@POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(@MultipartForm Document document) {
.....
}

my question is: how to send the same request using angularjs instead of using action="webresources/documents/upload" on my form. 我的问题是:如何使用angularjs发送相同的请求,而不是在表单上使用action =“ webresources / documents / upload”

You don't require a form tag, 您不需要表单标签,

This is how your HTML would look like, 这就是HTML的外观,

 <p>
        Select a file : 
        <input type="file" name="file" size="45" />
    </p>
<button ng-click="upload()">Upload</button>

Your controller code would be something like this, 您的控制器代码将是这样,

$scope.upload = function () {
  var fd = new FormData();
  fd.append('file', file);

  $http({
    url: 'webresources/documents/upload',
    data: fd,
    method: "POST",
    headers: {
      "Content-Type": undefined
    }
  });
}

This will upload your file to the server without form tags. 这会将您的文件上传到没有表单标签的服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM