AJAX file upload configuration

Upload file configuration

 


 

 

 

<!-- ajax file upload required -->
<script type="text/javascript" src="../js/jquery.form.js"></script>
<script type="text/javascript">
	$("#picForm").ajaxForm({
		url:"${ctx}/admin/goods/imgUpload",
		type:"POST",
		dataType:"text",
		async:true,
		success:function(){
			alert("Upload successful"+imgUrl);
tinyMCE.execCommand("mceInsertContent",false,"<img src=${ctx}/images/goodsDescImgs/"+imgUrl+">");
		},
		error:function(){
			alert("File upload failed!");
		}
	});	
</script>

 

 

 

 

<tr>
	<td class="tableleft"></td>
	<td>
	    <form id="picForm" method="POST" enctype="multipart/form-data">
	         <input type="file" name="pic"/>
	    </form>
	    <button type="submit" class="btn btn-primary" form="picForm">上传</button>
	    <button type="submit" class="btn btn-primary">保存</button>
	    <button type="button" class="btn btn-success" name="backid" id="backid">返回</button>
	 </td>
</tr>

  

 

Import the two prerequisite packages

commons-fileupload-1.2.jar

commons-io-1.4.jar 

 

 

Write in the configuration file of springMVC

 

<!-- Configuration file upload parser-->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- encoding, the default encoding is ISO-8859-1 -->
		<property name="defaultEncoding" value="UTF-8"></property>
		<!-- Configure the size of the file allowed to be uploaded, in bytes-->
		<property name="maxUploadSize" value="5848220"></property>
	</bean>

 

public dao configuration

 

 

public class BaseDao {
	private static SqlSessionFactory factory;
	//create local thread
	private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<>();
	//Initialize the SqlSessionFactory through a static block, because the static block will only be executed when the class is first loaded
	static {
		try {
			InputStream in = Resources.getResourceAsStream("Mybatis_config.xml");
			//Get the session of the database through the created factory object
			factory = new SqlSessionFactoryBuilder().build(in);
		} catch (IOException e) {
			e.printStackTrace ();
			System.err.println("Failed to get database connection factory!");
		}
	}
	/**
	 * Get the SqlSession object
	 * @return
	 */
	public static SqlSession getSqlSession(){
		//Get the sqlSession object through ThreadLocal
		SqlSession sqlSession = threadLocal.get();
		if(sqlSession == null){
			//Get a SqlSession object
			sqlSession = factory.openSession();
			//And stored in the ThreadLocal object, the thread synchronization management can be facilitated through the ThreadLocal object
			threadLocal.set(sqlSession);
		}
		return sqlSession;
	}
	/**
	 * Close the SqlSession object
	 */
	public static void closeSqlSession(){
		SqlSession sqlSession = threadLocal.get();
		if(sqlSession != null){
			sqlSession.close();
		}
		threadLocal.remove();
	}
	
}

  

 

    Configuration in action

 

public class GoodsAction {
	GoodsTypeDao goodsTypeDao = null;
	GoodsDao goodsDao = null;
	PageModel pageModel = new PageModel();
	/**
	 * ajax file upload
	 * @param file
	 * @param session
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/imgUpload")
	@ResponseBody
	public String fileUpload(@RequestParam(name="pic")MultipartFile file,HttpSession session) throws IOException{
		//Get the storage path of the file on the server
		String path = session.getServletContext().getRealPath("/images/goodsDescImgs");
		//Get the name of the uploaded file
		String fileName = file.getOriginalFilename();
		// do file storage
		file.transferTo(new File(path,fileName));
		return fileName;
	}

   

    dao configuration of goods 

 

public class GoodsDao extends BaseDao {
	private static final String NAMESPACE_NAME = "com.carshop.mapper.GoodsMapper.";
	
	/**
	 * New product
	 * @param goods
	 * @return true new addition succeeded false new addition failed
	 */
	public boolean saveGoods(Goods goods){
		SqlSession session = getSqlSession();
		int count = session.insert(NAMESPACE_NAME+"saveGoods",goods);
		session.commit();
		closeSqlSession();
		return count>0?true:false;
	}
}

 

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326452249&siteId=291194637