简体   繁体   English

如何在 Grails 中接收 Angular $http post 多部分表单数据

[英]How to receive Angular $http post multipart form data in Grails

How to receive angular $http post multipart form data from Grails.如何从 Grails 接收有角度的 $http post 多部分表单数据。 Here I have sent multipart form data from Angular controller to the Grails.在这里,我已将多部分表单数据从 Angular 控制器发送到 Grails。 I am new to Grails.我是 Grails 的新手。

Anyone can give me guidance to retrieve-boundary data.任何人都可以指导我检索边界数据。 I don't Know exactly it's correct form to receive image file data with some input data also.我不知道接收带有一些输入数据的图像文件数据到底是正确的形式。

Request headers in browser's network console:在浏览器的网络控制台中请求标头:

Provisional headers are shown
Accept:application/json, text/plain, */*
Content-Type:multipart/form-data; boundary=----    
WebKitFormBoundary0p6R8BecvYqzcbMK
Origin:file://
User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us)        
AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465     Safari/9537.53
Request Payload
------WebKitFormBoundary0p6R8BecvYqzcbMK
Content-Disposition: form-data; name="rackImage"; filename="PhoneGap.png"
Content-Type: image/png


------WebKitFormBoundary0p6R8BecvYqzcbMK
Content-Disposition: form-data; name="storeNo"

HD1304

------WebKitFormBoundary0p6R8BecvYqzcbMK
Content-Disposition: form-data; name="rackQty"

12
------WebKitFormBoundary0p6R8BecvYqzcbMK--

Here you go.干得好。 Just write the following in your controller:只需在您的控制器中写入以下内容:

class MyController {

    def upload() {
        def multipartFile = params.rackImage

        InputStream is
        FileOutputStream fos

        byte[] fileRead = new byte[1024]
        File tempFile

        try {
            is = multipartFile.getInputStream()
            String fileName = multipartFile.getOriginalFilename()

            String path = "./"

            fileName = fileName.replaceAll("[^a-zA-Z0-9//._-]+", "").toLowerCase()

            tempFile = new File(path + "" + fileName)
            fos = new FileOutputStream(tempFile);
            int i = is.read(fileRead)
            while (i != -1) {
                fos.write(fileRead, 0, i);
                i = is.read(fileRead);
            }
        } catch (FileNotFoundException e) {
            log.error "Exception uploading", e
        } catch (IOException e) {
            log.error "Exception uploading", e
        } finally {
            fos?.close()
            is?.close()
        }

        // Now access the File: "tempFile"
    }
}

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

相关问题 在 Angular 7.x 中接收格式化的“multipart/form-data”响应 - Receive formatted 'multipart/form-data' response in Angular 7.x 如何将文件发布到多部分/表单数据? - How to post files to a multipart/form-data? 我如何在angularjs中使用$ http.post()在post方法中使用enctype =“ multipart / form-data”发送表单数据 - how can i send the form data with enctype=“multipart/form-data” in the post method using $http.post() in angularjs 使用jQuery使用multipart / form-data进行HTTP POST调用? - Making an HTTP POST call with multipart/form-data using jQuery? 如何在角度2的http post请求中发送表单数据? - How to send form data in a http post request of angular 2? Angular中的$ http如何从表单数据发布到nodejs - $http in Angular how to post from form data in to nodejs $ http.post()的Codeigniter + Angular问题。 Codeigniter无法接收数据 - Codeigniter + Angular issue with $http.post(). Codeigniter not receive data 使用 multipart/form-data 查询 HTTP POST 请求的 Azure http-trigger 函数 - Azure http-trigger function to query HTTP POST request with multipart/form-data 如何使用AngularJS $ http发送multipart / form-data - How to use AngularJS $http to send multipart/form-data 如何在jQuery POST中将enctype作为multipart / form-data发送 - How to send enctype as multipart/form-data in jquery POST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM