0

how grails get file from xhr? my ajax

var xhr = new XMLHttpRequest()  
xhr.open("POST", "actionGrails", true)
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("X-File-Name", file.fileName);
xhr.setRequestHeader("X-File-Size", file.fileSize);
xhr.setRequestHeader("Content-Type", "multipart/form-data");
xhr.send(file);

when in my controller

def actionGrails() {
def f = request.getFile('file')
println f//get error
}

any solution for this? thanks

rsijaya
  • 159
  • 2
  • 14
  • `xhr.send(file)` is not a valid approach. see [here][1]. [1]: http://stackoverflow.com/questions/6211145/upload-file-with-ajax-xmlhttprequest – Brian Henry Jan 04 '13 at 16:11
  • hi brian, thx for you reply, but how can i access the file from formData with grails?, i have search but is always php example, i need grails example, thanks – rsijaya Jan 05 '13 at 06:49
  • you could try changing the accepted answer in the link, `formData.append("thefile", file);` to `formData.append("file", file);` to work with your action. All this is speculation, though. You should post the error you're getting, to get better answers. possibly also the declaration of 'file' in your client-side code. – Brian Henry Jan 05 '13 at 19:18
  • i just want to save the file to my folder using p.transferTo(new File('../LGN-cms/web-app/uploads/)) in grails, or can ajax transfer the file to my folder without grails command? because i dont need to save the file to my db, but to my specific folder. thanks – rsijaya Jan 06 '13 at 05:33

1 Answers1

2

Here is a simple solution: use Fine Uploader. It has a demo page.

The Grails plugin which packages the library is here .

Ray Nicholus
  • 19,538
  • 14
  • 59
  • 82
coderLMN
  • 3,076
  • 1
  • 21
  • 26
  • hi jinzhao, thx u reply, u reference it's to complex, u have any reference in grails? thanks – rsijaya Jan 05 '13 at 07:14
  • thanks for you answer, but i dont need plugin, i just want to know how grails get params.file from ajax/jquery command. thanks – rsijaya Jan 06 '13 at 05:37
  • Like other frameworks do, Grails got it from the form submitted from views. There should be some description in docs, depending on what lib/plugin you adopt. – coderLMN Jan 06 '13 at 07:08
  • yeah, but i use angular in frontend, and grails in backend, so i wonder to know how i pass params.file because i want to save the file in some folder, thanks anyway – rsijaya Jan 06 '13 at 07:46