簡體   English   中英

如何在 Grails 中接收 Angular $http post 多部分表單數據

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

如何從 Grails 接收有角度的 $http post 多部分表單數據。 在這里,我已將多部分表單數據從 Angular 控制器發送到 Grails。 我是 Grails 的新手。

任何人都可以指導我檢索邊界數據。 我不知道接收帶有一些輸入數據的圖像文件數據到底是正確的形式。

在瀏覽器的網絡控制台中請求標頭:

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--

干得好。 只需在您的控制器中寫入以下內容:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM