我必须向一个动作(Struts2)发送一些来自jsp的uppload文件。 运行良好。
<form action="uploads" method="post" enctype="multipart/form-data">
<input type="file" name="upload" >
<input type="file" name="upload" >
<input type="file" name="upload" >
<input type="submit" value="Go"/>
</form>
但是我需要在发送之前执行一些操作,所以我必须通过javascript发送:
var form = document.forms.namedItem("loadform");
form.enctype = "multipart/form-data";
form.method = "post";
var url = "./uploads";
form.action = url;
form.submit();
,其中
<form id="loadform" name="loadform" action="uploads" method="post" enctype="multipart/form-data">
<div id="attachs-<s:property value="#row.count"/>" style="width: 380px; position: relative; float: right; height: auto;"></div>
</form>
并使用dom填充DIV(我省略了一些步骤以达到js功能)):
function addInputFile(dad, baseid, idx, ww) {
var elem = null;
elem = document.createElement("input");
elem.setAttribute("type", "file");
elem.setAttribute("id", baseid+"FX-"+idx);
elem.setAttribute("name", "upload");
elem.style.width = ww+"px";
elem.style.fontSize = "11px";
dad.appendChild(elem);
}
但这不起作用!
操作未看到文件列表:
这是代码......
public String pdfUploads() {
try {
String targetPath = getText("pdf.repository");
logger.info("### Target Path: " + targetPath);
for (File u : uploads) {
logger.info("source *** " + u + "\t" + u.length());
}
for (String n : uploadFileNames) {
logger.info(" dest. *** " + n);
}
logger.info(" end params *** ");
File fileToCreate = null;
for (int i=0; i<uploads.size(); i++) {
File u = uploads.get(i);
String n = uploadFileNames.get(i);
fileToCreate = new File(targetPath, n);
FileUtils.copyFile(u, fileToCreate);
etc.
...这是日志(不考虑for循环中的记录器):
24/07 10:07:33| INFO [http-apr-10080-exec-6] (PratModAction.java:591) - ### Target Path: E:\JAVA\DLQ\pdfrepository
24/07 10:07:33| INFO [http-apr-10080-exec-6] (PratModAction.java:600) - end params ***
而且,很明显,文件没有加载到文件夹中! 为什么呢?
PS - 操作始终与检索html表单发送的文件相同!