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

4
  • xhr.send(file) is not a valid approach. see [here][1]. [1]: stackoverflow.com/questions/6211145/… Jan 4, 2013 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 5, 2013 at 6: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. Jan 5, 2013 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 6, 2013 at 5:33

1 Answer 1

2

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

The Grails plugin which packages the library is here .

4
  • hi jinzhao, thx u reply, u reference it's to complex, u have any reference in grails? thanks
    – rsijaya
    Jan 5, 2013 at 7: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 6, 2013 at 5: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 6, 2013 at 7: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 6, 2013 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.