问题
I'm trying to upload file using input of type file, also I have another param I want to submit
below my form code:
<form id="fileupload" action="uploadMassContentsFromExcelSheetNew" method="post"
enctype="multipart/form-data" onsubmit="return doMassValidate();" >
<input type="hidden" id="fileName" name="fileName" value=""/>
<input type="hidden" id="serviceIdMass" name="serviceIdMass" value=""/>
<input type="hidden" id="contentTypeMass" name="contentTypeMass" value=""/>
<div id='fileDiv' class="row fileupload-buttonbar">
<div class="col-md-12 col-xs-12 col-lg-12">
<div class="input-group input-group-file">
<input type="text" class="form-control"
disabled="disabled" id="uploadFile" name="uploadFile">
<span class="input-group-btn">
<span class="btn btn-primary btn-file">
<i class="icon md-upload" aria-hidden="true"></i>
<input type="file" name="files" id="uploadBtn">
</span>
</span>
<p style="word-wrap: break-word;">
<small id="fileError" style="display: none;"
class="help-block" data-fv-validator="notEmpty"
data-fv-result="VALIDATED">
<s:property value="getText('content.file.error')" />
</small>
</p>
</div>
</div>
</div>
<button name="sub" type="submit" >submit</button>
</form>
JS code:
function doMassValidate(){
var hasSelectedFile=checkIfHasSelectedFile();
if(!hasSelectedFile){
fileErrorMessage();
$("#fileError").hide();
return false;
}
else{
removeErrorClass('fileDiv');
return true;
}
}
function checkIfHasSelectedFile(){
var hasNotUploadFile= $('#uploadBtn').val();
if(hasNotUploadFile=="" || hasNotUploadFile=='' )
return false;
else return true;
}
this is my action:
public String uploadMassContentsFromExcelSheetNew() {
HSSFSheet sheet = null;
HSSFWorkbook workbook = null;
FileInputStream inputStream = null;
result=new JSONObject();
try {
if (fileName != null){
fileName=fileName.trim();
if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx") ) {
HttpServletRequest request = ServletActionContext.getRequest();
System.out.println("Parts*******:"+request.getParts()); //return null
MultiPartRequestWrapper multiWrapper=(MultiPartRequestWrapper)ServletActionContext.getRequest();
String fileNameParam=servletRequest.getParameter("fileName");//i get the file name value
String fileParam=servletRequest.getParameter("files");// i cant get param with name "files" when i inspect the servletRequest
Part filePart =request.getPart("files");//return null
File[] f=multiWrapper.getFiles("files"); //return null
String fileName=multiWrapper.getFileNames("files")[0];
Enumeration fileParameterNames = multiWrapper.getParameterNames();
String param = request.getParameter("files");
}
}
}
catch(Exception ex){
ex.printStackTrace();
}
}
and im using fileUpload interceptor:
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="basicStack"/>
this is the request:
------WebKitFormBoundaryjBpjIWpSdq4RzL0X Content-Disposition: form-data; name="fileName" C:\fakepath\SMSMassploadOladDate.xls ------WebKitFormBoundaryjBpjIWpSdq4RzL0X Content-Disposition: form-data; name="serviceIdMass" 647 ------WebKitFormBoundaryjBpjIWpSdq4RzL0X Content-Disposition: form-data; name="contentTypeMass" 1 ------WebKitFormBoundaryjBpjIWpSdq4RzL0X Content-Disposition: form-data; name="files"; filename="" Content-Type: application/octet-stream
Shouldn't file content type be "application/ms-excel,application/vnd.ms-excel
"
instead of application/octet-stream
? and filename shouldn't be empty?
I really cant get what is the wrong with files?
回答1:
i have solved the issue, in struts.xml i was just extends "struts-default",it should be extends "struts-default,json-default". also its related to blueimp plugin, i tried to use, "main.js" library was invoked and make my code not work.
来源:https://stackoverflow.com/questions/40627118/trying-to-upload-file-but-not-being-sent-by-the-request