问题
How do I browse to upload a file using Dojo? See the code below:
dojox.io.xhrMultiPart({
url: "http://localhost:8080/myWebService",
handleAs: "xml",
form: dojo.byId("myForm"),
load: function(data){ processRequest(data);},
error: function(error){ processError(error); },
backButton: function() {
},
mimetype: "text/xml"
});
Now which code do I have to append to the above code to upload a file?
回答1:
I use dojox.form.Uploader
, which will enable you the "Browse" functionality.
<input name="file" multiple="false" type="file" dojoType="dojox.form.Uploader" label="Select Class File" id="uploadedfile" />
I use dojo.io.iframe.send
to upload file.
Then on the submit button of the form, I use following function to post,
function addSwitchType(){
var td = dojo.io.iframe.send({
url: switchType_Add_URL,
form: formName,
method: "post",
preventCache: true,
handleAs: "json",
load: function(response, ioArgs){
},
error: function(response, ioArgs){
}
});
}
回答2:
You can use dojox.form.Uploader along with dojox.form.uploader.FileList. Just declare these 2 as follows.
up = new dojox.form.Uploader({
label: "Select files",
multiple: true,
class: "browseButton",
url: "UploadFile.php"
}).placeAt(form);
list = new dojox.form.uploader.FileList({
uploader: up
}).placeAt(form);
btn = new Button({
label: "upload",
onClick: function() {
up.upload();
}
}).placeAt(form);
btn.startup();
up.startup();
list.startup();
来源:https://stackoverflow.com/questions/11219973/how-to-upload-file-using-dojo