我在jsp中使用隐藏的iframe来上传图像文件。代码如下:
<form name="frm" method="POST" action="./servlet/newSubmit1">
<div>
Image:
<input type="file" id="uploadImage" name="uploadImage" size="30" onchange="uploadFiles(); fileUpload(this.form,'./servlet/TempImageUploader','upload123')"/>
<input type="hidden" id="UserName" value="TestValue"/>
</div>
<div id="upload123"></div>
<ul>
<li>
<input type="button" value="Upload" tabindex="6" onclick="uploadAttachedPhotos()"/>
</li>
</ul>
</form>
正如您在代码中看到的那样,调用浏览按钮的onchange事件“uploadFiles()”和“fileUpload”函数被调用。 uploadFiles 函数使用 DWR 将文件上传到服务器上的temp目录并在jsp页面上预览, fileUplaod 函数创建一个隐藏的iframe调用servlet,它将所有上传的图像保存在地图中。我在页面上也有一些其他数据可以由用户更新。例如,这里我有“UserName”字段,可以有文件名。
创建隐藏iframe的javascript函数是:
function fileUpload(form, action_url, div_id) {
// Create the iframe...
var iframe = document.createElement("iframe");
iframe.setAttribute("id", "upload_iframe");
iframe.setAttribute("name", "upload_iframe");
iframe.setAttribute("width", "0");
iframe.setAttribute("height", "0");
iframe.setAttribute("border", "0");
iframe.setAttribute("style", "width: 0; height: 0; border: none;");
// Add to document...
form.parentNode.appendChild(iframe);
window.frames['upload_iframe'].name = "upload_iframe";
iframeId = document.getElementById("upload_iframe");
// Add event...
var eventHandler = function () {
if (iframeId.detachEvent) iframeId.detachEvent("onload", eventHandler);
else iframeId.removeEventListener("load", eventHandler, false);
// Message from server...
if (iframeId.contentDocument) {
content = iframeId.contentDocument.body.innerHTML;
} else if (iframeId.contentWindow) {
content = iframeId.contentWindow.document.body.innerHTML;
} else if (iframeId.document) {
content = iframeId.document.body.innerHTML;
}
document.getElementById(div_id).innerHTML = content;
// Del the iframe...
setTimeout('iframeId.parentNode.removeChild(iframeId)', 250);
}
if (iframeId.addEventListener) iframeId.addEventListener("load", eventHandler, true);
if (iframeId.attachEvent) iframeId.attachEvent("onload", eventHandler);
// Set properties of form...
form.setAttribute("target", "upload_iframe");
form.setAttribute("action", action_url);
form.setAttribute("method", "post");
form.setAttribute("enctype", "multipart/form-data");
form.setAttribute("encoding", "multipart/form-data");
// Submit the form...
document.forms[0].action = action_url;
document.forms[0].submit();
document.getElementById(div_id).innerHTML = "Uploading...";
}
现在的问题是,当我按下上传按钮时,为什么我在父窗体的操作servlet上获得request.getParameter("UserName")
NULL?
谢谢