如何将文件上传到服务器目录.. 如果我的项目在D:\ myapp,我用cmd运行d:\ myapp grails run-app 当我运行这个应用程序和其他计算机运行它并上传文件..将在目录D:\ myapp \ upload?中保存ini计算机服务器?
我试试这个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)
答案 0 :(得分:15)
目的地只是像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)
OR 如果您想将其存储在相对于用户家的某个路径:
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,则应提交表单,而不是添加指向其他页面的链接。
答案 1 :(得分:2)
Grails应用的位置无关紧要。您必须在控制器中指定完整的目标路径。这是一个例子
def upload() {
def f = request.getFile('filecsv')
if (f.empty) {
flash.message = 'file cannot be empty'
render(view: 'uploadForm')
return
}
f.transferTo(new File('D:\myapp\upload\file_name.txt'))
response.sendError(200, 'Done')
}
答案 2 :(得分:0)
最终字符串IMAGE_DIR =“ $ {servletContext.getRealPath('/ images')} /”;
def employeeId = "dablu_photo";
def employeePicture = request.getFile("cv_");
String photoUrl ="";
if (employeePicture && !employeePicture.empty) {
if (new java.io.File(IMAGE_DIR+"/employee_photo/"+employeeId+".png")?.exists()){
FileUtils.deleteQuietly(new java.io.File(IMAGE_DIR+"/employee_photo/"+employeeId+".png"));
}
employeePicture.transferTo(new java.io.File(IMAGE_DIR+"/employee_photo/"+employeeId+".png"))
}