2016-03-18 26 views
0

我正尝试上传文件到服务器。这是我的表单代码。在POST中没有收到任何数据

<form method="POST" action="<%request.getContextPath();%>/OPMS/webl" enctype="multipart/form-data"> 


<input type="hidden" name="handler" value="fileupload" /> 
<input type="hidden" name="user" value="${dev.getUserId()}" /> 
<input type="file" id="fileLoader" name="files" title="Load File" /> 
<button type="button" class="btn btn-primary" onClick="openFileChooser();"><em class="fa"></em> Choose File</button> 
<button type="submit" class="btn btn-primary"><em class="fa fa-upload"></em> Upload </button> 
</form> 

问题是我无法从属性中获取数据。当我检查处理程序时,它返回null。但如果我改变方法GET我可以正确查看请求中的参数,但获取将无法使用的表单enctype的类型。

+0

您是否收到错误消息?我们需要查看您正在使用的代码来尝试并接收文件。 – jagler

+0

我尝试请求时收到空指针异常。 getParameter(“handler”)。equals(“fileupload”) –

回答

0

如果你只是希望把工作做好,我建议Ravishanker库苏马的Hayageek jQuery File Upload plugin

http://hayageek.com/docs/jquery-upload-file.php

他打破了过程分为三个简单的步骤,基本上是这样的:

<head> 
    <link href="http://hayageek.github.io/jQuery-Upload-File/uploadfile.min.css" rel="stylesheet"> // (1) 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
    <script src="http://hayageek.github.io/jQuery-Upload-File/jquery.uploadfile.min.js"></script> // (1) 
</head> 
<body> 
    <div id="fileuploader">Upload</div> // (2) 
    <script> 
     $(document).ready(function(){ 
      $("#fileuploader").uploadFile({ // (3) 
       url:"my_php_processor.php", 
       fileName:"myfile" 
      }); 
     }); 
    </script> 
</body> 

最后一步是让jQuery代码中指定的PHP文件(在本例中为my_php_processor.php)接收并处理文件:

my_php_processor.php:

<?php 
    $output_dir = "uploads/"; 
    $theFile = $_FILES["myfile"]["name"]; 
    move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir.$fileName); 

myfile注之间的PHP($_FILES["myfile"])和jQuery代码块中指定的文件名的关系。

+0

我使用jsp ..那么,什么类型必须是文件? –

+0

对不起,我应该注意到,在你的代码中,它应该在问题(标签)中指定。此解决方案可能无法为您工作。 – gibberish