试图上传文件,但没有被请求发送 [英] trying to upload file, but not being sent by the request
问题描述
我正在尝试使用类型文件的输入上传文件,还有另一个参数,我想在表单代码下面提交
:
< form id =fileuploadaction =uploadMassContentsFromExcelSheetNewmethod =
enctype =multipart / form-dataonsubmit =return doMassValidate(); >
< input type =hiddenid =fileNamename =fileNamevalue =/>
< input type =hiddenid =serviceIdMassname =serviceIdMassvalue =/>
< input type =hiddenid =contentTypeMassname =contentTypeMassvalue =/>
< 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 =textclass =form-control
disabled =disabledid =uploadFilename =uploadFile>
< span class =input-group-btn>
< span class =btn btn-primary btn-file>
< i class =icon md-uploadaria-hidden =true>< / i>
< input type =filename =filesid =uploadBtn>
< / span>
< / span>
< p style =word-wrap:break-word;>
class =help-blockdata-fv-validator =notEmpty
data-fv-result =VALIDATED>
< s:property value =getText('content.file.error')/>
< / small>
< / p>
< / div>
< / div>
< / div>
< button name =subtype =submit> submit< / button>
< / form>
JS代码:
function doMassValidate(){
var hasSelectedFile = checkIfHasSelectedFile();
if(!hasSelectedFile){
fileErrorMessage();
$(#fileError)。hide();
返回false;
}
else {
removeErrorClass('fileDiv');
返回true;
$ b函数checkIfHasSelectedFile(){
var hasNotUploadFile = $('#uploadBtn')。val();
if(hasNotUploadFile ==|| hasNotUploadFile =='')
return false;
else返回true;
$ p这是我的动作:
public String uploadMassContentsFromExcelSheetNew(){
HSSFSheet sheet = null;
HSSFWorkbook workbook = null;
FileInputStream inputStream = null;
result = new JSONObject();
尝试{
if(fileName!= null){
fileName = fileName.trim();如果(fileName.endsWith(。xls)|| fileName.endsWith(。xlsx)){
HttpServletRequest request = ServletActionContext.getRequest();
System.out.println(Parts *******:+ request.getParts()); //返回null
MultiPartRequestWrapper multiWrapper =(MultiPartRequestWrapper)ServletActionContext.getRequest();
字符串fileNameParam = servletRequest.getParameter(fileName); //我得到文件名值$ b $字符串fileParam = servletRequest.getParameter(文件); / /我不能得到参数与名称文件当我检查servletRequest
Part filePart = request.getPart(files); //返回null
文件[] f = multiWrapper.getFiles(files); //返回null
String fileName = multiWrapper.getFileNames(files)[0];
枚举fileParameterNames = multiWrapper.getParameterNames();
String param = request.getParameter(files);
$ b catch(Exception ex){
ex.printStackTrace();
和im使用fileUpload拦截器:
< interceptor-ref name =fileUpload/>
< interceptor-ref name =basicStack/>
这是要求:
------ 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 = 文件;文件名=
内容类型:application / octet-stream
不应将文件内容类型设为 application / ms-excel,application / vnd.ms-excel
而不是应用/八位字节流
?和文件名不应该为空?
我真的不知道文件有什么问题吗?
我已经解决了这个问题,在struts.xml中,我只是扩展了struts-default,它应该扩展struts-default,json-default。
也与blueimp插件相关,我试图使用main.js库被调用,并使我的代码无法正常工作。
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?
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.
这篇关于试图上传文件,但没有被请求发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!