[分享] input拍照上傳圖片方法分享,可單圖與多圖

來源:互聯網
上載者:User

標籤:

示範用,比較簡陋,勿怪,方法簡單!
input被點擊之後預設出現拍照,文檔等選項,可以拍照上傳,也可以選圖庫檔案上傳,也可以錄影進行視頻上傳,自己對應修改檔案類型判斷就可以了
這些功能無需調用cordova外掛程式

如果需要使用外掛程式拍照上傳,使用java作為後端的,可以參考 http://bbs.wex5.com/forum.php?mod=viewthread&tid=91095
該方法是把圖片base64編碼成二進位流,可以存進資料庫,調用的時候解碼顯示
使用php等其他非java作為後端的,同樣可以把圖片base64編碼化,直接像存字串那樣存進資料庫

1.構建

 

對應源碼

<div class="x-panel-content" xid="content1"><form xid="postForm" accept="image/jpeg,image/png" enctype="multipart/form-data" target="postResultIframe" class="form-horizontal container-fluid" method="post" action="http://127.0.0.1"><input type="file" name="photo" xid="uploadfile" bind-change="uploadfileChange"/><button xid="button1" bind-click="button1Click" type="submit">提交</button></form>


那裡面那個action=127.0.0.1那個,改成自己的請求地址

如果需要多檔案上傳,input需要類似這樣構建

<input type=‘file‘ name=‘photo1‘><input type=‘file‘ name=‘photo2‘><input type=‘file‘ name=‘photo3‘>

 

或者

<input type=‘file‘ name=‘photo[]‘><input type=‘file‘ name=‘photo[]‘><input type=‘file‘ name=‘photo[]‘>

 

2.input綁定bind-change="uploadfileChange"
作用是監控input,看注釋很清楚了

Model.prototype.uploadfileChange = function(event) {if (!event.target.files) { // 如果客戶沒有選擇相關檔案,直接返回return;}var uploadimage = $(this.getElementByXid(‘uploadfile‘)); // 用jQuery拿到input標籤 var file = event.target.files[0]; // 拿到使用者選擇的檔案 if (/^image\/\w+$/.test(file.type)) { // 如果是圖片檔案this.isimg = true;} else { // 如果使用者選的的不是圖片檔案justep.Util.hint(‘請選擇圖片檔案!‘);$uploadimage.val(‘‘);}};

 

3.上主菜
我的後端使用php做的,使用其他後端的,自行找一個對應的上傳類對接就可以了

Model.prototype.button1Click = function(event) { var form = this.getElementByXid("postForm"); // 拿到form表單的js對象var acturl = "http://127.0.0.1/index.php/home/index/uploadcar";form.attributes["action"].value = require.toUrl(acturl);// 提交表單$(form).submit(function() {$(this).ajaxSubmit(function(resultJson) {alert(resultJson);});return false; // 阻止表單預設提交}); }; 

 


4. require("./baasd/jquery.form"); 路徑改成自己的
http://pan.baidu.com/s/1eSgDluE

5.加上後端吧,thinkphp 3.2的

public function uploadcar() {$upload = new \Think\Upload(); // 執行個體化上傳類$upload->maxSize = 1024 * 1000 * 10; // 設定附件上傳大小$upload->exts = array(‘jpg‘, ‘gif‘, ‘png‘, ‘jpeg‘); // 設定附件上傳類型$upload->rootPath = ‘./upload/img_zj/‘; //證件目錄$upload->savePath = ‘‘; // 設定附件上傳目錄$upload->autoSub = true;$upload->subName = array(‘date‘, ‘Y/m/d‘);$upload->saveRule = date("YmdHis", time()) . "_" . mt_rand(1000, 9999); //上傳名已時間戳記儲存// 上傳檔案$info = $upload->upload();if (!$info) {//上傳失敗 // 上傳錯誤提示錯誤資訊$this->error($upload->getError());} else {//上傳成功$imgpath = ‘/upload/img_zj/‘ . $info[‘photo‘][‘savepath‘] . $info[‘photo‘][‘savename‘];echo $imgpath;}}

 

我這裡直接返回的就是圖片地址,你可以把3裡面直接改造,類似我這樣寫法

var xszimg = this.getElementByXid("xszimg");$(form).submit(function() {$(this).ajaxSubmit(function(resultJson) {$(xszimg).attr("src", transURL(resultJson));$(xszimg).show();});return false; // 阻止表單預設提交});

 

這樣直接就把圖片顯示出來了

[分享] input拍照上傳圖片方法分享,可單圖與多圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.