1

如何接收來自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-- 
+0

聽起來好像您正在嘗試檢索數據,但是您正在使用POST請求?這不是寧靜的方式。通常,HTTP GET用於從服務器獲取數據,POST用於將數據發送到服務器。 – James

+0

你的'$ http.post'看起來像什麼?您在控制器操作中迄今爲止嘗試過什麼? – tylerwal

回答

0

在這裏你去。只需在控制器中寫下以下內容:

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" 
    } 
} 
相關問題