gpt4 book ai didi

grails - 如何使用grails将文件上传到服务器目录?

转载 作者:行者123 更新时间:2023-12-02 13:46:23 25 4
gpt4 key购买 nike

如何将文件上传到服务器目录..
如果我的项目在 D:\myapp 并且我使用 cmd d:\myapp grails run-app 运行
当我运行这个应用程序和其他计算机运行它并上传文件时..它会将 ini 计算机服务器保存在目录 D:\myapp\upload 中?

我试试这个ini gsp。

<g:form action="list" enctype="multipart/form-data" useToken="true">
<span class="button">
<input type="file" name="filecsv"/>
<input type="button" class="upload" value="Upload"
onclick='location.href = "${createLink(url: [action: 'upload'])}"'/>
</span>
</g:form>

def upload = {

def f = request.getFile('filecsv')
if (f.empty) {
flash.message = 'file cannot be empty'
render(view: 'list')
return
}

f.transferTo(new File('C:\Users\meta\Documents\workspace-sts-2.5.2.RELEASE\wawet\wallet\uploads\file_name.csv'))
response.sendError(200, 'Done')
}

这是错误:
2014-02-03 10:43:02,706 [http-8080-2] ERROR errors.GrailsExceptionResolver  - No signature of method: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [filecsv]
Possible solutions: getXML(), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getJSON(), getHeader(java.lang.String)
groovy.lang.MissingMethodException: No signature of method: org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestWrapper.getFile() is applicable for argument types: (java.lang.String) values: [filecsv]
Possible solutions: getXML(), getAt(java.lang.String), getAt(java.lang.String), getLocale(), getJSON(), getHeader(java.lang.String)
at com.teravin.wallet.LoanAccountController$_closure12.doCall(com.teravin.wallet.LoanAccountController:308)
at com.teravin.wallet.LoanAccountController$_closure12.doCall(com.teravin.wallet.LoanAccountController)
at java.lang.Thread.run(Thread.java:744)

最佳答案

目标只是一个像 Java 中的文件。

def f = request.getFile('some_file')

//validate file or do something crazy hehehe

//now transfer file
File fileDest = new File("Path to some destination and file name")
f.transferTo(fileDest)

如果要将其存储在相对于用户主页的某个路径中:
def homeDir = new File(System.getProperty("user.home")) //user home e.g /home/username for unix
File fileDest = new File(homeDir,"path/to/some_folder")
f.transferTo(fileDest)

更新
根据您的原因 getFile不起作用,您没有提交表单:
<g:form action="list" enctype="multipart/form-data" useToken="true">

<span class="button">
<input type="file" name="filecsv"/>
<input type="button" class="upload"
value="Upload"
onclick='location.href = "${createLink(url: [action: 'upload'])}"'/>

</span>

</g:form>

应该:
<g:form action="upload" enctype="multipart/form-data" useToken="true">

<span class="button">
<input type="file" name="filecsv"/>
<input type="submit" class="upload" value="upload"/>

</span>

</g:form>

如果您需要使用 javascript,您应该提交表单而不是添加到另一个页面的链接。

关于grails - 如何使用grails将文件上传到服务器目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21519144/

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