Dynamic input file Multiple files uploaded to the background does not respond to the solution!!!

Source: Internet
Author: User
Tags dateformat save file

In fact, I also do not clear what the specific reason, but the back can be!!!

I'm using Springmvc's own file upload.

1, the first must be to have SPRINGMVC upload files related configuration!

2. Front-end

This is the dynamic input file upload to the background does not respond to the wording (page write dead upload to the background is possible)

This piece of code is written in the form form under table>>.

<input type= "button" name= "button" value= "Add Attachment" onclick= "Addinput ()" >
<input type= "button" name= "button" value= "Delete Attachment" onclick= "Deleteinput ()" >
<span id= "Upload" ></span>
Switch
Bar above that code in a different way, first write <form>, write <table>

<div class= "modal-content" style= "width:600px" >
<form action= "/ecp/mulfileupload/testfu" method= "POST" enctype= "Multipart/form-data" >
<input type= "hidden" name= "TopicID" value= "${topicid}" >
<table class= "Table table-bordered" >
<tr>
Number of participants in <th> activities:</th>
<td><input type= "text" name= "Peoplecount"/></td>
</tr>
<tr>
:</th> per capita funding for <th> activities
<td><input type= "text" name= "Perprice"/></td>
</tr>
<tr>
<th> upload images/Accessories:</th>
<td>
<input type= "File" name= "Myfiles"/>
<span id= "Upload" ></span>
</td>
</tr>
<tr>
<td>
<input type= "button" name= "button" value= "Add picture/Attachment" onclick= "Addinput ()" >
<input type= "button" name= "button" value= "Delete picture/Attachment" onclick= "Deleteinput ()" >
</td>
<%--<td><button class= "btn btn-success" onclick= "Formsubmit ()" ></button></td>--%>
<td><input type= "Submit" class= "btn btn-success"/></td>
</tr>

</table>
</form>
</div>

That's okay, honestly I don't know why (!!!!) )

3. JS Code


var attachname = "Myfiles";

function Addinput () {
Createinput (Attachname);
}
function Deleteinput () {
Removeinput ();
}

function Createinput (name) {
var aelement=document.createelement ("input");
Aelement.name=name;
aelement.type= "File";

var spanelement = document.getElementById ("Upload");
/* IF (document.getElementById ("Upload"). InsertBefore (aelement,spanelement.nextsibling) = = null) {
return false;
}*/
if (document.getElementById ("Upload"). AppendChild (aelement) = = null) {
return false;
}
return true;
}

function Removeinput () {
var aelement = document.getElementById ("Upload");
if (Aelement.removechild (aelement.lastchild) = = null) {
return false;
}

return true;
}

4. Java code

Package Com.ibm.db.controller;

Import Com.ibm.db.service.IMulFileUploadService;
Import Com.ibm.db.service.ITopicService;
Import Org.apache.commons.io.FileUtils;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestParam;
Import Org.springframework.web.bind.annotation.ResponseBody;
Import Org.springframework.web.bind.annotation.RestController;
Import Org.springframework.web.multipart.MultipartFile;

Import Javax.annotation.Resource;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Java.io.File;
Import java.io.IOException;
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;

/**
* Created by zml on 16-4-11.
*/
@RestController
@RequestMapping (value = "/ecp/mulfileupload")
public class Mulfileloadifycontroller {

@Resource (name = Imulfileuploadservice.service_name)
Private Imulfileuploadservice Topicservice;

@RequestMapping (value = "/testfu")
Public String AddUser (@RequestParam multipartfile[] myfiles, HttpServletRequest request) throws IOException {

Date datatime = new Date ();
Save the information about the activity post, and change the material submission status to 1
Topicservice.insertino (Topicid,1,peoplecount,perprice,datatime);

If you are uploading only one file, you only need to receive the file Multipartfile type, and you do not need to explicitly specify @requestparam annotations
If you want to upload multiple files, you will need to use the multipartfile[] type to receive the file, and also specify the @requestparam annotation
And when uploading multiple files, all <input type= "file"/> in the foreground form should be myfiles, otherwise the myfiles in the parameter cannot get all the uploaded files.
Determine that the file array cannot be empty and is longer than 0
if (myfiles!=null&&myfiles.length>0) {

Loop to get the files in the file array
for (int i =0;i<myfiles.length;i++) {
Multipartfile file = Myfiles[i];
String Uploadcontenttype =file.getcontenttype ();
String expandedname = "";
if (Uploadcontenttype.equals ("Imagepeg")
|| Uploadcontenttype.equals ("Image/jpeg")) {
IE6 upload jpg image headimagecontenttype is imagepeg, and IE9 and Firefox upload jpg image is Image/jpeg
Expandedname = ". jpg";
} else if (Uploadcontenttype.equals ("Image/png")
|| Uploadcontenttype.equals ("Image/x-png")) {
IE6 uploaded PNG image headimagecontenttype is "image/x-png"
Expandedname = ". png";
} else if (Uploadcontenttype.equals ("Image/gif")) {
Expandedname = ". gif";
} else if (Uploadcontenttype.equals ("Image/bmp")) {
Expandedname = ". bmp";
}

Save File
SaveFile (file,expandedname,request);
}
}
return "uploadsuccess";
return "redirect:/list.html";
}

/***
* Save File
* @param file
* @return
*/
Private Boolean SaveFile (Multipartfile file,string expandedname,httpservletrequest request) {
DateFormat df = new SimpleDateFormat (Topiccontroller.default_sub_folder_format_auto);
String fileName = Df.format (New Date ());
Determine if the file is empty
if (!file.isempty ()) {
try {
String FilePath = "";
File Save path
if (Expandedname!=null&&!expandedname.equals ("")) {
If it's a picture
FilePath = Request.getsession (). Getservletcontext (). Getrealpath ("/") + "upload/img/"
+ Filename+expandedname;
}else{
String originalfilename = File.getoriginalfilename ();
String suffix=originalfilename.substring (Originalfilename.lastindexof (".") +1);
FilePath = Request.getsession (). Getservletcontext (). Getrealpath ("/") + "upload/file/"
+ filename+ "." +suffix;
}


File TargetFile = new file (FilePath);
if (!targetfile.exists ()) {
Targetfile.mkdirs ();
}

Dump files
File.transferto (targetfile);
return true;
} catch (Exception e) {
E.printstacktrace ();
}
}
return false;
}
}

Dynamic input file Multiple files uploaded to the background does not respond to the solution!!!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.