jquery-file-upload demo

來源:互聯網
上載者:User

標籤:style   blog   http   color   java   io   檔案   for   

:http://plugins.jquery.com/blueimp-file-upload/

文檔地址:https://github.com/blueimp/jQuery-File-Upload/wiki

本文只是一個demo,實現功能也很簡單:點擊一個上傳按鈕,使用者選擇圖片,圖片Ajax上傳到後台儲存到硬碟,返回一個串連,頁面上顯示使用者上傳的圖片。

(jquery-file-upload多檔案上傳可以做的非常漂亮,我這裡有點大材小用了)

註:demo 頁面為jsp,服務端為Java springMVC。

頁面代碼:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html><html xmlns="http://www.w3.org/1999/html"><head>    <meta charset="UTF-8">    <title>file upload demo</title>        <link rel="stylesheet" href="<c:url value =‘/resource/themes/bootstrap/css/bootstrap.min.css‘/>">    <link rel="stylesheet" href="<c:url value =‘/resource/js/jQuery-File-Upload/css/jquery.fileupload.css‘/>">     <script src="<c:url value =‘/resource/js/jquery-1.9.1.min.js‘/>"></script>    <script src=" <c:url value =‘/resource/js/jQuery-File-Upload/js/vendor/jquery.ui.widget.js‘/>"></script>    <script src=" <c:url value =‘/resource/js/jQuery-File-Upload/js/jquery.iframe-transport.js‘/>"></script>    <script src=" <c:url value =‘/resource/js/jQuery-File-Upload/js/jquery.fileupload.js‘/>"></script>    <script src="<c:url value =‘/resource/themes/bootstrap/js/bootstrap.min.js‘/>"></script>         <script type="text/javascript">         $(function () {            var url = "/portal/upload/uploadImg.do";            $(‘#fileupload‘).fileupload({                url: url,                dataType: ‘text‘,                done: function (e, data) {                    alert(1);                    console.dir(data);                    $(".imgView img").attr(‘src‘,‘${contextPath}‘+data.result);                    $("#progress").hide();                    $(".imgView").show();                                    },                progressall: function (e, data) {                    console.dir(data);                    var progress = parseInt(data.loaded / data.total * 100, 10);                    $(‘#progress .progress-bar‘).css(                        ‘width‘,                        progress + ‘%‘                    );                }            });        });    </script></head><body>    <span class="btn btn-success fileinput-button">        <i class="glyphicon glyphicon-plus"></i>        <span>Select file.</span>        <!-- The file input field used as target for the file upload widget -->        <input id="fileupload" type="file" name="imgFile"/>    </span>    <br>    <br>    <!-- The global progress bar -->    <div id="progress" class="progress">        <div class="progress-bar progress-bar-success"></div>    </div>    <div class="imgView" hidden="hidden">        <img src="">    </div>            </body></html>

如果要自己寫css

那麼只需以下四個js:jquery.min.js、jquery.ui.widget.js、jquery.iframe-transport.js 、jquery.fileupload.js

配置:對id為fileupload 的file input 進行fileupload執行個體化,url即為圖片要上傳到服務端的後台連結,也可以通過html5的屬性 data-url 直接在input中給,input的name要給,貌似不能再配置中配,這個很不方便。progressall是配置進度條的,done是上傳到後台返回後的事件。

Java代碼:

 

import java.io.IOException;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;import com.isoftstone.veco.common.base.controller.BaseController;import com.isoftstone.veco.common.upload.FileUploadHandler;@RequestMapping("/upload")@Controllerpublic class UploadController extends BaseController{    @RequestMapping("/uploadImg.do")    public @ResponseBody    String uploadMaterialImg(@RequestParam("imgFile") MultipartFile multipartFile)    {        String resp = null;        if (multipartFile != null)        {            try            {                byte[] file = multipartFile.getBytes();                String dir = "/test";                String imgId = FileUploadHandler.uploadImg(file, dir);                resp = FileUploadHandler.UPLOAD_CONFIG.getImgVirtualDir() + "?" + FileUploadHandler.UPLOAD_CONFIG.getDownloadParamName() + "="                        + imgId;            } catch (IOException e)            {                e.printStackTrace();            }        }        return resp;    }}

 

try中間的上傳邏輯就不詳細寫了,controller這裡寫的有點挫,應該返回一個json格式的,判斷上傳是否成功,給使用者反饋的,不過我這裡只是一個demo,哈哈

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.