WeX5中input拍照上传图片方法分享,可单图与多图

演示用,比较简陋,勿怪,方法简单!
input被点击以后默认出现拍照,文档等选项,能够拍照上传,也能够选图库文件上传,也能够录像进行视频上传,本身对应修改文件类型判断就能够了
这些功能无需调用cordova插件php

若是须要使用插件拍照上传,使用java做为后端的,能够参考 http://bbs.wex5.com/forum.php...
该方法是把图片base64编码成二进制流,能够存进数据库,调用的时候解码显示
使用php等其余非java做为后端的,一样能够把图片base64编码化,直接像存字符串那样存进数据库java

1.构建如图
图片描述jquery

对应源码ajax

<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那个,改为本身的请求地址thinkphp

若是须要多文件上传,input须要相似这样构建数据库

<input type='file' name='photo1'>后端

<input type='file' name='photo2'>post

<input type='file' name='photo3'>ui

或者this

<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; // 阻止表单默认提交

               });

};

  1. 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; // 阻止表单默认提交

                       });

这样直接就把图片显示出来了

相关文章
相关标签/搜索