PC端上傳圖片的功能
1. 點選圖片觸發上傳的事件
2. 上傳圖片
3. 返回圖片路徑展示
具體程式碼為:
1. 在物件上繫結點選事件
<img src="" alt="點選上傳" id="faceImg" style="border: 1px solid; width: 50px; height: 50px; padding: 5px; cursor: pointer;" onclick="toUpload();" />
2. 事件內容為
其中的getImgPath為獲取歷史圖片路徑,有時可能需要刪除歷史圖片function toUpload() { jQuery('#oldFilePath').val(getImgPath()); jQuery('#fileUpload').click(); }
function getImgPath() {
return jQuery('#faceImg').attr('src').replace(extApi.getWebRoot(), '');
}
3.將上傳圖片的動作放在隱藏域裡面,這樣用於隱藏上傳按鈕,而且上傳檔案成功後也不會發生頁面的跳轉或重新整理,這裡才是真正的上傳動作的發起者
<form id="formFile" method="post" enctype="multipart/form-data" target="frameFile" > <input type="file" accept="image/*" id="fileUpload" name="fileUpload" class="input-file" onchange="uploadFile();"> <input type="hidden" value="" id="oldFilePath" name="oldFilePath"> </form>
4.上傳圖片的功能,上傳圖片到服務端並返回圖片路徑展示在頁面
function uploadFile() { var form = jQuery("#formFile"); form.ajaxSubmit({ url : 'upload_img_used.action', type : 'post', beforeSubmit : function() { layer.load(); }, success : function(data) { layer.closeAll('loading'); jQuery('#faceImg').attr('src', extApi.getWebRoot() + data); } }); }
5.java端程式碼為:
public class FileUpload { private static Logger logger = Logger.getLogger(FileUpload.class); /** * 伺服器檔案上傳總資料夾名 */ public static final String UPLOAD = "upload" + File.separator; /** * 預設表單檔名 */ public static final String INPUT = "fileUpload"; /** * 上傳檔案,隨機生成檔名,採用預設檔案表單name * * @param request * @param uploadFolder * 上傳目標目錄 * @return * @throws FrameworkException */ public static String upload(HttpServletRequest request, String uploadFolder) throws FrameworkException { return uploadFile(request, INPUT, uploadFolder, false, null); } /** * 上傳檔案,隨機生成檔名 * * @param request * @param imgInput * 檔案表單name * @param uploadFolder * 上傳目標目錄 * @return * @throws FrameworkException */ public static String upload(HttpServletRequest request, String imgInput, String uploadFolder) throws FrameworkException { return uploadFile(request, imgInput, uploadFolder, false, null); } /** * 上傳檔案,檔名隨機生成,採用預設表單name, 刪除舊檔案 * * @param request * @param uploadFolder * 上傳目標路徑 * @param oldPath * 舊檔案路徑 * @return * @throws FrameworkException */ public static String uploadDelOld(HttpServletRequest request, String uploadFolder, String oldPath) throws FrameworkException { return uploadFile(request, INPUT, uploadFolder, false, oldPath); } /** * 上傳檔案,檔名隨機生成,刪除舊檔案 * * @param request * @param inputName * 檔案表單name * @param uploadFolder * 上傳目標路徑 * @param oldPath * 舊檔案路徑 * @return * @throws FrameworkException */ public static String uploadDelOld(HttpServletRequest request, String inputName, String uploadFolder, String oldPath) throws FrameworkException { return uploadFile(request, inputName, uploadFolder, false, oldPath); } /** * 上傳檔案,指定檔名,採取預設表單name * * @param request * @param uploadFolder * 上傳資料夾 * @param fileName * 檔名 * @return * @throws FrameworkException */ public static String uploadHasName(HttpServletRequest request, String uploadFolder) throws FrameworkException { return uploadFile(request, INPUT, uploadFolder, true, null); } /** * 上傳檔案,並指定檔名 * * @param request * @param inputName * 檔案表單name * @param uploadFolder * 上傳目錄 * @param fileName * 檔名 * @return * @throws FrameworkException */ public static String uploadHasName(HttpServletRequest request, String inputName, String uploadFolder) throws FrameworkException { return uploadFile(request, inputName, uploadFolder, true, null); } /** * 上傳檔案,指定檔名稱,並刪除舊檔案 * * @param request * @param inputName * 檔案表單name * @param uploadFolder * 上傳目錄 * @param fileName * 檔名 * @param oldPath * 舊檔案路徑 * @return * @throws FrameworkException */ public static String uploadHasName(HttpServletRequest request, String inputName, String uploadFolder, String oldPath) throws FrameworkException { return uploadFile(request, inputName, uploadFolder, true, oldPath); } /** * 獲取表單中檔案資料內容 * * @param request * @param inputName * 檔案表單name * @param uploadFolder * 上傳目標目錄 * @param fileName * 檔名 * @param oldPath * 舊檔案路徑 * @return * @throws FrameworkException */ private static String uploadFile(HttpServletRequest request, String inputName, String uploadFolder, boolean isHasName, String oldPath) throws FrameworkException { // 獲取上傳的資料內容 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile(inputName); if (null == file) { return null; } return uploadFile(request, file, uploadFolder, isHasName, oldPath); } /** * 將檔案資料存入伺服器 * * @param request * @param file * 檔案資料內容 * @param upLoadPath * 上傳目標路徑 * @param fileName * 檔名 * @param oldPath * 舊檔案路徑 * @return * @throws FrameworkException */ private static String uploadFile(HttpServletRequest request, CommonsMultipartFile file, String upLoadPath, boolean isHasName, String oldPath) throws FrameworkException { try { // 拼湊檔案儲存到服務端本地的路徑 String strRoot = getRootUrl(request); // 若存在舊路徑且檔案存在,則刪除舊檔案 if (!StringUtils.isNullOrEmpty(oldPath) && FileUtils.isExist(strRoot + oldPath)) { delImg(request, oldPath); } // 獲取檔名 String fileName = file.getOriginalFilename(); // 不保留檔名則隨機生成檔名 if (!isHasName) { // 獲取字尾 String suffix = FileUtils.getFileExt(file.getOriginalFilename()); fileName = EnvUtils.getHashId(); // 與字尾拼接 fileName += suffix; } logger.info("檔名: " + fileName); // 獲取路徑 String filePath = getFilePath(strRoot, upLoadPath, fileName); // 若檔案存在,則刪除 if (FileUtils.isExist(filePath)) { FileUtils.delete(filePath); } // 上傳檔案 FileUtils.save(file.getInputStream(), filePath); // 獲取返回路徑 filePath = filePath.replace(strRoot, ""); // 清除伺服器根目錄 filePath = filePath.replaceAll("\\\\", "/"); // 瀏覽器和linux中路徑使用反斜槓. logger.info("檔案已上傳: " + filePath); return filePath; } catch (Exception e) { logger.error("上傳圖片失敗", e); throw new FrameworkException("上傳檔案失敗", e); } } /** * 獲取當前伺服器根路徑 * * @param request * @return */ private static String getRootUrl(HttpServletRequest request) { return request.getSession().getServletContext().getRealPath("/"); } /** * 轉化出一個資料夾名 * * @param folderName * @return */ public static String toFolder(String folderName) { return folderName + File.separator; } /** * 拼接檔案上傳路徑 * * @param strRoot * 伺服器根路徑 * @param upLoadPath * 上傳路徑 * @param fileName * 檔名 * @return */ private static String getFilePath(String strRoot, String upLoadPath, String fileName) { StringBuffer path = new StringBuffer(strRoot); path.append(UPLOAD); path.append(upLoadPath); path.append(fileName); return path.toString(); } /** * @param path * @param request * @throws FrameworkException */ public static void delImg(HttpServletRequest request, String path) throws FrameworkException { String strRoot = getRootUrl(request); FileUtils.delete(strRoot + path); logger.info("刪除舊檔案: " + strRoot + path); } }
相關推薦
PC端上傳圖片的功能
功能實現的邏輯是:1. 點選圖片觸發上傳的事件 2. 上傳圖片 3. 返回圖片路徑展示 具體程式碼為: 1. 在物件上繫結點選事件 <img src="" alt="點選上傳" id="faceImg" style="border: 1px solid; widt
PC端上傳頭像及裁剪功能
基於vue框架 藉助elementUI框架做為上傳元件 藉助vue-cropper做為裁剪工具 話不多說,先看效果: 上傳頭像元件: <div class="app-head"> <h3>小程式頭像<
微信小程式上傳圖片功能(附後端程式碼)
幾乎每個程式都需要用到圖片,在小程式中我們可以通過image元件顯示圖片。 當然小程式也是可以上傳圖片的,微信小程式文件也寫的很清楚。 上傳圖片 首先選擇圖片 通過wx.chooseImage(OBJECT)實現 官方示例程式碼 ? 1 2
cropper.js 移動端上傳圖片 並 裁剪 的功能實現
定好頁面 效果如圖 要求 點選 + 號之後 上傳圖片 裁剪之後 放在頁面上 實現 依賴 remodal 和 cropper.js 把裁剪的內容 放在remodal裡 點選 +
jquery 手機端上傳圖片
圖片格式 正方 res ide -h wid kit can ati 需要引入exif.js,矯正iphone拍攝圖片方向 function readImage(file) { if (!/image\/\w+/.test(file.type)) {a
怎樣實現前端裁剪上傳圖片功能
cos pass iframe 上傳文件 repl 轉變 除了 mimetype 處理 怎樣實現前端裁剪上傳圖片功能 2017-05-20 JavaScript 由於前端是不能直接操作本地文件的,要麽通過<input type="file">用戶點擊選擇文
Django 實現上傳圖片功能
文件添加 css doc += href pip http sys 直接 很多時候我們要用到圖片上傳功能,如果圖片一直用放在別的網站上,通過加載網址的方式來顯示的話其實也挺麻煩的,我們通過使用 django-filer 這個模塊實現將圖片文件直接放在自己的網站上。 感興
上傳圖片功能
HTML <img src="img/[email protected]" onclick="browerfile.click()" id="QRCode" /> <input type="file" id="browerfile" style="disp
winform端上傳圖片至flask伺服器
winform端程式碼: public static string PostImageData(string url, IDictionary<string, string> parameters, int timeout, string userAgent, CookieColle
FastDFS的Python客戶端 -- 通過客戶端上傳圖片並訪問
本文的前提是已經啟動FastDFS的tracker和storage ㈠ 安裝 將檔案 fdfs_client-py-master.zip 存放在pycharm中,然後再終端進行安裝: pip install fdfs_client-py-master.zip (安裝包後
部分vivo和oppo手機,使用上傳圖片功能,可能會出現退出webview的現象(回退到app的入口頁面)
在公司的app裡面嵌入了一個h5頁面,h5頁面有個使用圖片上傳功能,上傳圖片出現閃退的現象 問題描述: vivo手機,在app內的wap頁面使用上傳圖片的功能,在選擇好圖片點選確認按鈕後,出現退出整個webview,回退到app該wap頁面的入口頁面,且app自動重新整理了該入口頁面
上傳圖片功能實現
1.如果沒有相機許可權,申請開啟相機許可權 if (!ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.CAMERA)){ ActivityCompat.requestPe
上傳圖片功能的實現
html 中 < form method="POST" action="..." enctype="multipart/form-data"> < div class="box" style="padding:20px 25px;"> // 隱藏域傳 ti
手機端上傳圖片(單圖)非外掛
手機端上傳圖片 HTML <form action="" method="post" enctype="multipart/form-data"> <div class="uploader" style="display: none;z-
spring boot + vue新增上傳圖片功能
餘近日開發spring boot +vue的後臺管理專案,涉及到檔案上傳功能,使用之前專案的檔案上傳模組,一直有問題。遂經過兩天的百度,加個人理解,最終解決了基本的檔案上傳功能。 首先,html頁面: <!--form中是要加這個enctype的-->
Laravel5.2中使用xheditor編輯器實現上傳圖片功能
安裝xheditor 去官網下載一個安裝包(有詳細的安裝辦法),簡單說就是把xheditor_emot,xheditor_lang,xheditor_plugins,xheditor_skin四個資料夾放到你的js目錄下,把xheditor.js也要放在js資料夾下 但是,
thinkphp3.2.3 ueditor1.4.3 圖片上傳操作,線上刪除上傳圖片功能。
<?php namespace PublicClass; /** * Ueditor外掛 * @author Nintendov */ class Ueditor { //public $uid;//要操作的使用者id 如有登入需要則去掉註釋 private $output; //
Android端上傳圖片到後臺,儲存到資料庫中
首先點選頭像彈出popwindow,點選相簿,相機,呼叫手機自帶的裁剪功能,然後非同步任務類訪問伺服器,上傳頭像,儲存到資料庫中, 下面寫出popwindow的程式碼 //設定popwindow public PopupWindow getPopWindow
Django在admin後臺整合TinyMCE富文字編輯器新增上傳圖片功能
部落格網站怎麼能少了圖片,為TinyMCE編輯器新增上傳圖片功能。第一步:定義表存圖片路徑models.pyclassAdminIMG(models.Model): filename = mode
KindEditor富文本框編輯器上傳圖片功能實現,基於java項目
ger char 大小 append 參考 java ont area reat 1. HTML標簽與jquery代碼 <textarea id="editor_id" style="width: 200px; height: 200px;"></text