0
真的有什麼用?我有一個request/xhr模塊,它不能發佈multipart/form-data(兩個文本字段和一個文件上傳)。當使用Iframe時,我可以發佈沒有問題的表單。我想使用xhr。另一方面,Iframe無法使用,因爲我無法讀取響應主體。 xhr是否支持multipart/for-data?是否有另一種方法來執行這個簡單的任務:發佈一個文件和兩個名稱並獲得JSONObject響應。我正在閱讀部分文件。Dojo 1.9.3 xhr vs iframe
*************************** Dojo Code ***************************
<script>dojoConfig = {parseOnLoad: true}</script>
<script src='dojo-release-1.9.3/dojo/dojo.js'></script>
</head>
<body class="claro">
<form data-dojo-type="dijit/form/Form" id="myForm" data-dojo-id="myForm"
encType="multipart/form-data" action="" method="">
<label for="firstname">FirstName</label><input data-dojo-
type="dijit/form/ValidationTextBox" type="text" name="fname"/>
<label for="lastname">LastName:</label><input data-dojo-
type="dijit/form/ValidationTextBox" type="text" name="lname" />
<label for="fileName">Certificate Upload:</label><input type="file" name="fileName" />
<button type="button" id="startButton">Start</button> </form>
<script>
require(["dojo/request/xhr", "dojo/dom", "dojo/dom-form", "dojo/dom-construct",
"dojo/json", "dojo/on", "dojo/domReady!"],
function(xhr, dom, domForm, domConst, JSON, on){
on(dom.byId("startButton"), "click", function(){
var data = domForm.toObject("myForm");
xhr("addUser",{
data: data,
method:"post",
handleAs:"json"
}).then(function(cert){
alert("data received!");
}, function(err){
alert("data denied!!!");
alert(err); }); }); });
********************* servlet代碼***************** *******
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String FirstName = request.getParameter("fname"); // Retrieves <input type="text"
name="description">
String LastName = request.getParameter("lname"); // Retrieves <input type="text"
name="description">
Part Certfile = request.getPart("fileName"); // Retrieves <input type="file"
name="file">
String Certname = getFilename(Certfile); // Calls getFilename method
InputStream Certcontent = Certfile.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(Certcontent));
雖然一切順利,iframe。不幸的是,我無法讀取響應的身體.. :( – ysabz