gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:19:11 26 4
gpt4 key购买 nike

如何从 Grails 接收 Angular $http post 多部分表单数据。在这里,我将多部分表单数据从 Angular Controller 发送到 Grails。我是 Grails 的新手。

任何人都可以指导我检索边界数据。我也不知道接收带有一些输入数据的图像文件数据的正确形式。

浏览器网络控制台中的请求 header :

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

最佳答案

给你。只需在您的 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"
}
}

关于javascript - 如何在 Grails 中接收 Angular $http post 多部分表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33808967/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com