1. 程式人生 > >html頁面上傳圖片並進行展示

html頁面上傳圖片並進行展示

html頁面:

本功能實現圖片上傳並顯示,點選“檢視”按鈕也顯示圖片

<div class="form-group">
<label class="col-md-2 control-label">縮圖</label>
<div class="col-md-2">
<input type="hidden" name="news.thumbnail_atta_id" value="${news.thumbnail_atta_id!}" id="thumbnail_atta_id"/>
<div class="col-md-2">
@ if (!isEmpty(news.thumbnail_atta_id)) {
<div class="row" style="margin-bottom:5px;">
<button type="button" class="btn btn-primary" id="showThumbnail">檢視</button>
</div>
@ }
<div class="row">
<span class="btn btn-success fileinput-button">
    <i class="glyphicon glyphicon-plus"></i>
    <span>上傳...</span>
    <input id="fileupload" type="file" data-url="/atta/upload/news-thumbnail" multiple>
</span>
</div>
</div>
<div>
<img src="" id="thumbnail" width="150" height="100" style="display:none;"/>
</div>
</div>
</div>

js程式碼:

<script type="text/javascript">

//縮圖檔案上傳
$('#fileupload').fileupload({
dataType: 'json',
add: function(e, data) {
var acceptFileTypes = /(\.|\/)(gif|jpe?g|png)$/i;
if(data.originalFiles[0]['type'].length && !acceptFileTypes.test(data.originalFiles[0]['type'])) {
alert('請上傳圖片格式的檔案!')
} else {
data.submit();
}
},
done: function (e, data) {
$('#thumbnail_atta_id').val(data.result.id);
$('#thumbnail').attr('src', data.result.url).show();
}
});
//檢視縮圖
$('#showThumbnail').click(function(){
var src = $('#thumbnail').attr('src');
var id=$('#thumbnail_atta_id').val();
if (!src) {
$.get('/atta/show/${news.thumbnail_atta_id!}', function(res) {
var url=localurl+res[0].url;
//var url = "www.baidu.com/img/bd_logo1.png";
alert(url);
$('#thumbnail').attr('src', url).show();
}, 'json')
}
})

 </script>

java程式碼:

Controller類:

/**
* 上傳並返回附件ID及URL
*/

public void upload() {
UploadFile file = getFile();
String fileName = file.getFileName();
String fileSuffix = getSuffix(fileName);
String module = getPara(0);
String category = getPara(1);

String id = AttachmentService.me.uploadFile(file, module, category);
Map<String, Object> json = new HashMap<String, Object>();
json.put("id", id);
json.put("url", "/upload/" + category + "/" + module + "/" + id + fileSuffix);
json.put("name", fileName);

renderJson(json);
}

/**
* 返回給定ID的附件的訪問URL
*/
public void show() {
String ids = getPara();
if (null == ids) {
ids = getPara("ids");
}
String[] idArr = ids.split(",");

StringBuffer sql = new StringBuffer();
sql.append(" select * from attachment where id in ( ");
for (int index = 0; index < idArr.length; index++) {
sql.append(" ? ");
if (index != idArr.length - 1) {
sql.append(" , ");
}
}
sql.append(" ) ");

List<Map<String, Object>> json = new ArrayList<Map<String,Object>>();
List<Attachment> list =  Attachment.me.find(sql.toString(), (Object[])idArr);

//轉換成JsonArray
String url = null;
for (Attachment atta : list) {
url = getAttaURL(atta);
Map<String, Object> item = new HashMap<String, Object>();
item.put("id", atta.getStr("id"));
item.put("url", url);
item.put("name", atta.getStr("name"));
json.add(item);
}
renderJson(json);
}

private String getAttaURL(Attachment atta) {
StringBuffer path = new StringBuffer();
path.append(File.separator + "upload" + File.separator);
path.append(atta.getStr("category"));
path.append(File.separator);
path.append(atta.getStr("module"));
path.append(File.separator);
path.append(atta.getStr("id"));
path.append(getSuffix(atta.getStr("name")));
return path.toString();
}

private String getSuffix(String name) {
return name.replaceAll(".*(\\..*)", "$1");
}

service類:

/**
* 上傳檔案
* @param file uploadFile
* @param module模組,如news/cases/doctor/
* @param category分類,如縮圖thumbnail/頭像avatar/幻燈片ppt
* @return 附件主鍵
* @throws IOException 
*/
public String uploadFile(UploadFile file, String module, String category) {
String attachmentId = CommonUtils.getUUID();
File f = file.getFile();

new Attachment().set("id", attachmentId)
.set("name", file.getOriginalFileName())
.set("size", f.length())
.set("content_type", file.getContentType())
.set("module", module)
.set("category", category)
.set("upload_time", new Date())
.save();
try {
//重新命名
String dirPath = file.getSaveDirectory();
File renamedFile = new File(dirPath + attachmentId + file.getOriginalFileName().replaceAll(".*(\\..*)", "$1"));
FileUtils.moveFile(f, renamedFile);
//PPT需要轉換成圖片,拷貝一份到轉換目錄(/upload/trans/ppt)
if (category.equals("ppt")) {
FileUtils.copyFileToDirectory(renamedFile, new File(dirPath + "trans/ppt/"), true);
}
//拷貝到對應的目錄下
FileUtils.moveToDirectory(renamedFile, new File(dirPath + category + File.separator + module), true);
} catch(Exception e) {
e.printStackTrace();
}
return attachmentId;
}

相關推薦

html頁面圖片進行展示

html頁面: 本功能實現圖片上傳並顯示,點選“檢視”按鈕也顯示圖片 <div class="form-group"><label class="col-md-2 control-label">縮圖</label><div clas

html js 圖片回顯

<html> <body>     <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>    <scr

HTML頁面圖片直接預覽

<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>上傳圖片</title> </head> <b

圖片圖片展示頁面

html需要一個上傳的input,一個canvas ​ <input type="file" id="img"/><br /><br /><br /> <canvas id="canvas"></canvas> inp

基於VUE選擇圖片頁面顯示(圖片可刪除)

.ajax sta http data .cn 數據 file prim 生成 demo例子: 依賴文件 : http://files.cnblogs.com/files/zhengweijie/jquery.form.rar HTML文本內容:

微信js圖片 展示,iphone下預覽

https useragent 預覽圖 shang 服務器 fun 相冊 put 相機 $(‘.addphotos‘).click(function(){ var that = $(this);

使用ajaxSubmit非同步圖片展示

頁面在選擇圖片完成後將圖片上傳到圖片伺服器,並在頁面顯示圖片,然後將上傳圖片的路徑載入到隱藏域中,提交表單時將路徑儲存到資料庫。 頁面程式碼: <script> function sub

html圖片顯示

function loadFile() { try { //獲取上傳的檔案 var file = document.getElementById(

PHP.25-TP框架商城應用實例-後臺1-添加商品功能、鉤子函數、在線編輯器、過濾XSS、圖片生成縮略圖

引用傳遞 none move 名稱 textarea 如果 library time fields 添加商品功能   1、創建商品控制器【C】  /www.test.com/shop/Admin/Controller/GoodsController.class.php

3種圖片實現預覽的方法

load app chunks isp 賦值 response with span attr 在常見的用戶註冊頁面,需要用戶在本地選擇一張圖片作為頭像,並同時預覽。 常見的思路有兩種:一是將圖片上傳至服務器的臨時文件夾中,並返回該圖片的url,然後渲染在html頁面;另一種

selenium實戰 二 進入博客園,圖片發帖

編輯 sendkeys arr accept 圖片 .cn ledir car spa 1 #-*- coding:utf-8 -*- 2 __author__ = "carry" 3 4 from selenium import webdriver 5 im

html   圖片,本頁預覽

html 上傳圖片 本頁預覽 直接上代碼<!DOCTYPE html><html><head><meta charset="UTF-8"><title>圖片上傳預覽</title><script type="text/j

js圖片預覽

width view 直接 ali 上傳圖片並預覽 class tar result null 一:html代碼 <input id="upload_image" type="file" accept="image/*" capture="camera"onch

圖片預覽

div str mms con PE onload pac att nload <div class="mms_uploadIconBox" > <input class="mms_add_imgBox" style="opaci

Winform下如何圖片顯示出來。同時保存到數據庫

內存 pbm 命令 數據 lena bms south 類對象 private 通常,我們在開發軟件或者網站是否,通常有時候需要添加圖片,我們怎麽做呢,直接貼例子。 前提是添加openFileDialog控件哈 #region 定義公共的類對象及變量 S

H5 中html 頁面存為圖片長按 保存

成功 實現 原因 ati data 比較 編碼解碼 轉換 萬能 最近接到的一個新需求:頁面一個靜態H5,中間有一頁是輸入信息,然後跳轉到最後一頁,自動將頁面生成圖片,用戶可以長按圖片保存到手機上。 展示一下最後一頁的樣子: 剛拿到這個需求,在網上看了很多文章,最普遍的是

圖片顯示

步驟: 用建構函式實體化new一個檔案讀取器FileReader      2.用實體化出來的檔案讀取器呼叫readAsDataURL方法,將檔案物件作為引數傳進去,讀取檔案為dataURL(要先呼叫一下這個函式才能在下一步使用result) &n

簡單實現前端選擇圖片顯示略縮圖

效果 實現程式碼 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>修改資訊</title> <sc

thinkPHP利用ajax非同步圖片顯示、刪除

近來學習tp5的過程中,專案中有個發帖功能,選擇主題圖片。如下: 利用原始的檔案上傳處理,雖然通過原始js語句能實時顯示上傳圖片,但是這樣的話會涉及很多相容問題。使用ajax技術,實現選擇性刪除所選圖片功能,並不會有相容問題。 表單檔案form: &lt;form method="po

SSM圖片儲存圖片地址到資料庫

說出來有點不好意思,還沒寫過上傳圖片的功能。最近接的一個外包專案因為有要用到這個功能,所以打算使用SSM實現圖片上傳的功能,上傳好後把圖片地址儲存到資料庫,同時在前端顯示圖片。 使用maven構建專案,首先匯入相關的jar,這裡就放上傳檔案的jar配置,不然篇幅太長了,其他的還有sp