我files and other fields data
使用 ajax 将两者都发送到 servlet FormData append()
,如下所示:
html
<form id="formId1" >
<input type="file" name="file" id="fileid">
<input type="text" name="t1" id="d">
<input type="submit" id="btn" value="submit">
</form>
阿贾克斯调用:
$("#btn").click(function(event){
event.preventDefault();
var fd = new FormData();
var other_data = $('form').serializeArray();
$.each(other_data,function(key,input){
fd.append("t1",$("#d").val());
fd.append("file",$('#fileid').prop('files')[0])
});
$.ajax({
url: 'Sample1',
data: fd,
contentType: false,
processData: false,
type: 'POST',
success: function(data){
console.log(data);
}
}); });
我@MultipartConfig
以前都读过files and other data
@WebServlet("/Sample1")
@MultipartConfig
public class Sample1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String s=request.getParameter("t1");
System.out.println(s);
Part part=request.getPart("file");
File path =new File("path")// HOW TO PUT PART INTO THE FOLDER
}}
现在无法将该部分作为图像存储到文件夹中如何获取folder or images
请Part
帮助我。