I try to upload a file to a moodle2 container. This is done via yui. To skip this stuff I disable java script in my WebClient (browser) to get the "noscript" elements. After analysing these I get to a page where the filepicker.php of moodle is called.
The page looks like this.
<form action="http://demo.moodle.net/repository/filepicker.php?ctx_id=41&itemid=861552155&env=editor&course=2&maxbytes=67108864&areamaxbytes=-1&maxfiles=-1&subdirs=1&sesskey=chtRoKYBlC&action=browse&draftpath=%2F&savepath=%2F&repo_id=3" method="post" enctype="multipart/form-data" style="display:inline">
<label>Attachment: </label><input name="repo_upload_file" type="file"><br>
<input name="action" value="upload" type="hidden"><br>
<input name="draftpath" value="/" type="hidden"><br>
<input name="savepath" value="/" type="hidden"><br>
<input name="repo_id" value="3" type="hidden"><br>
<input value="Upload this file" type="submit">
</form>
Complete case against demo.moodle.org (manually create Folder with Name "Test" in Mooodle-Course 'My first course' id=2 first):
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlFileInput;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSpan;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MoodleUpload {
/** Instance of WebClient */
private static final WebClient browser = new WebClient();
/** Current HTMLPage */
private HtmlPage currentPage;
public MoodleUpload(){
//intialize WebClient
browser.getOptions().setJavaScriptEnabled(true);
browser.getOptions().setRedirectEnabled(true);
browser.getOptions().setThrowExceptionOnScriptError(false);
browser.getOptions().setCssEnabled(false);
browser.getOptions().setUseInsecureSSL(true);
browser.getOptions().setThrowExceptionOnFailingStatusCode(false);
browser.getCookieManager().setCookiesEnabled(true);
try{
login();
currentPage = (HtmlPage) browser.getPage("http://demo.moodle.net/course/view.php?id=2");
modifyFolder(getFolderByName("Test"));
} catch (Exception e){
Logger.getLogger(MoodleUpload.class.getName()).log(Level.SEVERE, null, e);
}
}
//perform login on moodle
private void login() throws Exception{
currentPage = (HtmlPage) browser.getPage("http://demo.moodle.net/");
//get login-Form, fill in required fields and perform click
((HtmlTextInput) currentPage.getElementById("login_username")).setValueAttribute("manager");
((HtmlPasswordInput) currentPage.getElementById("login_password")).setText("sandbox");
List<HtmlSubmitInput> inputs;
inputs = (List) currentPage.getByXPath("//form/div/input[@type='submit']");
inputs.get(0).click();
}
//finds link to folder with given name
private String getFolderByName (String name) throws Exception{
List<HtmlSpan> spans;
spans = (List) currentPage.getByXPath("//a/span[@class='instancename' and text()='"+name+"']");
System.out.println(((HtmlAnchor) spans.get(0).getEnclosingElement("a")).getHrefAttribute());
return ((HtmlAnchor) spans.get(0).getEnclosingElement("a")).getHrefAttribute();
}
//trys to upload a file to a moodle folder
private void modifyFolder(String path) throws Exception{
//disable javascript to get noscript elements
browser.getOptions().setJavaScriptEnabled(false);
currentPage = (HtmlPage) browser.getPage(path);
//click 'edit settings'
List<HtmlAnchor> anchors;
anchors = (List) currentPage.getByXPath("//ul/li/p[@class='tree_item leaf']/a[text()='Edit settings']");
currentPage = (HtmlPage) browser.getPage(anchors.get(0).getHrefAttribute());
//get file uploader
List<HtmlElement> up;
up = (List) currentPage.getByXPath("//noscript/div/object");
currentPage = (HtmlPage) browser.getPage(up.get(0).getAttribute("data"));
//get "add file"-Page
List<HtmlElement> elements;
elements = (List) currentPage.getByXPath("//div[@class='filemanager-toolbar']");
currentPage = (HtmlPage) browser.getPage(((HtmlAnchor) elements.get(0).getFirstChild()).getHrefAttribute());
elements = (List) currentPage.getByXPath("//a[text()='Upload a file']");
currentPage = (HtmlPage) browser.getPage(((HtmlAnchor) elements.get(0)).getHrefAttribute());
//get file upload form
elements = (List) currentPage.getByXPath("//form");
HtmlForm uploadForm = (HtmlForm) elements.get(0);
HtmlFileInput fileInput = uploadForm.getInputByName("repo_upload_file");
fileInput.setValueAttribute("file:///F:/Test.xlsx");
fileInput.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//submit
List<HtmlSubmitInput> inputs;
inputs = (List) currentPage.getByXPath("//form/input[@type='submit']");
currentPage = inputs.get(0).click();
System.out.println(currentPage.asText());
}
public static void main(String[] args){
MoodleUpload tl = new MoodleUpload();
}
}
If I check the current page I get a success message from moodle. But the file is not in the destinated container. I tried several modifications to my code and to the action parameter of the form corresponding to the 'documentation' of filepicker.php to no avail. If I fill out the form in Firefox, everything works fine. If I skip the setValue-Call, I also get a failure message. So something seems to happen.
Maybe this is associated to the additional hidden fields? Any help is appreciated. I would also use javascript and the yui-functionality, if this would be a feasible solution.
I solved the problem in my moodle installation. Unfortunately this doesn't do the trick on demo.moodle.net. What I do is simple: I get the HtmlSubmitInput with the id "id_submitbutton" (=save) from the edit page. After uploading the file as I have shown, I just call click() from this submit. After that, the file is transfered from moodle draft to manager.