springMVC上传图片,使用jquery预览

首先在html加入代码:其中div用于显示上传后的图片预览

<form id="form" enctype="multipart/form-data">
        upload image:
        <input type="file" id="image_input" name="file" />
        <a href="#" onclick="upload()">upload</a>
    </form>
    <div id="imgDiv"></div>

js代码如下:需要先引入jquery.form.js插件<script type="text/javascript" src="js/jquery-form.js"></script>使用ajaxSubmit

function upload() {
        var imagePath = $("#image_input").val();
        if (imagePath == "") {
            alert("please upload image file");
            return false;
        }
        var strExtension = imagePath.substr(imagePath.lastIndexOf(‘.‘) + 1);
        if (strExtension != ‘jpg‘ && strExtension != ‘gif‘
                && strExtension != ‘png‘ && strExtension != ‘bmp‘) {
            alert("please upload file that is a image");
            return false;
        }
        $("#form").ajaxSubmit({
            type : ‘POST‘,
            url : ‘upload/image.do‘,
            success : function(data) {
                alert("上传成功");
                $("#imgDiv").empty();
                $("#imgDiv").html(‘<img src="‘+data+‘"/>‘);
                $("#imgDiv").show();
            },
            error : function() {
                alert("上传失败,请检查网络后重试");
            }
        });

    }

服务器端代码:

package com.shop.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
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.multipart.MultipartFile;

@Controller
@RequestMapping("/upload/")
public class ImageUploadController {
    /**
     * upload image and return the image url
     *
     * @return
     * @throws IOException
     * @throws IllegalStateException
     */
    @RequestMapping("image")
    @ResponseBody
    public String uploadImage(HttpServletRequest request,
            HttpServletResponse response, HttpSession session,
            @RequestParam(value = "file", required = true) MultipartFile file)
            throws IllegalStateException, IOException {
        String path = session.getServletContext().getRealPath("/upload");
        System.out.println("real path: " + path);
        String fileName = file.getOriginalFilename();
        System.out.println("file name: " + fileName);
        File targetFile = new File(path, fileName);
        if (!targetFile.exists()) {
            targetFile.mkdirs();
        }
        file.transferTo(targetFile);
        String fileUrl = request.getContextPath() + "/upload/" + fileName;
        return fileUrl;
    }
}
时间: 2024-08-09 08:21:46

springMVC上传图片,使用jquery预览的相关文章

HTML5 CSS3 经典案例:无插件拖拽上传图片 (支持预览与批量) (二)

转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/31513065 上一篇已经实现了这个项目的整体的HTML和CSS: HTML5 CSS3 经典案例:无插件拖拽上传图片 (支持预览与批量) (一) 这篇博客直接在上篇的基础上完成,最终效果: 效果图1: 效果图2: 好了,请允许我把图片贴了两遍,方便大家看效果了~ 可以看出我们的图片的li的html其实还是挺复杂的,于是我把html文档做了一些修改: <span style=&quo

jquery 预览提交的表单

预览表单,查看后确认提交或者返回重填 演示 XML/HTML Code <form class="mform" id="myform" method="POST" id="myform" action=""> <fieldset> <legend>Registeration</legend> <table cellspacing="0"

3种上传图片并实现预览的方法

在常见的用户注册页面,需要用户在本地选择一张图片作为头像,并同时预览. 常见的思路有两种:一是将图片上传至服务器的临时文件夹中,并返回该图片的url,然后渲染在html页面:另一种思路是,直接在本地内存中预览图片,用户确认提交后再上传至服务器保存. 这两种方法各有利弊,方法一很明显,浪费流量和服务器资源:方法二则加重了浏览器的负担,并且对浏览器的兼容性要求更高(在某些低版本中的IE浏览器不支持). 以下是实现上述思路的方法: 1. 模板文件 1 <!DOCTYPE html> 2 <ht

在tp下UMeditor上传图片成功,预览失败

我的是tp3.2  上传编辑器图片时候处理的图片的类用的是UM自带的imagUp.class.php, 将UMeditor1.2.2放到  shop/Public/statics/下面 在上传图片的时候图片显示 用bug工具查看显示图片路径少了shop(项目根路径) 需要修改UM的配置文件umeditor.config.php 修改  ,imagePath:'/shop/Upload/image/umeditor/' <?php window.UMEDITOR_CONFIG = { //为编辑器

javascript和HTML5上传图片之前实现预览效果

一:FileList对象与file对象 FileList对象表示用户选择的文件列表,在HTML4中,file控件内只允许放置一个文件,但是到了HTML5中,通过添加multiple属性,file控件内允许一次放置多个文件,控件内的每一个用户选择的文件都是一个file对象,而FileList对象是file对象的列表: 比如如下代码: 选择文件<input type="file" id="file" multiple size="80"/>

JQuery预览图片

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <meta http-equiv="Content

页面上传图片后立即预览

页面上的应用大多都有在个人信息管理中设置头像的这么一个功能,而对于实现这个功能前提就是上传头像的功能,而当上传完成即可预览是所有后续操作的前提.在网上巴拉了一番,然后整理了一下,拼凑了一个小demo供大家学习.本博文出自博客园,作者Red,联系邮箱 [email protected],转载请保留本文原文链接http://www.cnblogs.com/itred/p/5723864.html. 这个demo基本可以实现页面上打开页面后,点击上传头像图片的按钮,经过选择,上传成功后即可在页面指定位

C#.net4建设网页上传图片前客户端预览图片不能显示

搞了好半天,完全照教学资料设的javascrip脚本,但就是显示不了,后来才发觉,原来客户端要把该网站设为信任才能显示. 所以记录一下,以后备查. <script type="text/javascript">        function showphoto() {            alert(document.getElementById("FileUpload1").value);            //把所选图片的全路径+文件名以“f

js上传图片前进行预览

<script type="text/javascript"> //图片上传预览    IE是用了滤镜. function previewImage(file) { var MAXWIDTH = 260; var MAXHEIGHT = 180; var div = document.getElementById('preview'); if (file.files && file.files[0]) { div.style.display = "