gpt4 book ai didi

thinkphp jquery实现图片上传和预览效果

转载 作者:qq735679552 更新时间:2022-09-27 22:32:09 27 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章thinkphp jquery实现图片上传和预览效果由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

先上效果图:

thinkphp jquery实现图片上传和预览效果

那个file按钮样式先忽略 。

点击选择图片(浏览),随便选一张图片 。

thinkphp jquery实现图片上传和预览效果

js代码如下 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
//上传图片立即预览
  function previewimage(imgfile) {
   var filextension = imgfile.value.substring(imgfile.value
     .lastindexof( "." ), imgfile.value.length);
   filextension = filextension.tolowercase();
   if ((filextension != '.jpg' ) && (filextension != '.gif' )
     && (filextension != '.jpeg' ) && (filextension != '.png' )
     && (filextension != '.bmp' )) {
    alert( "对不起,系统仅支持标准格式的照片,请您调整格式后重新上传,谢谢 !" );
    imgfile.focus();
   } else {
    var path;
    if (document.all) //ie
    {
     imgfile.select();
     path = document.selection.createrange().text;
     document.getelementbyid( "photo_info" ).innerhtml = "" ;
     document.getelementbyid( "photo_info" ).style.filter = "progid:dximagetransform.microsoft.alphaimageloader(enabled='true',sizingmethod='scale',src=\""
       + path + "\")" ; //使用滤镜效果 
    } else //ff
    {
     path = window.url.createobjecturl(imgfile.files[0]); // ff 7.0以上
     //path = imgfile.files[0].getasdataurl();// ff 3.0
     document.getelementbyid( "photo_info" ).innerhtml = "<img id='img1' width='120px' height='100px' src='" +path+ "'/>" ;
     //document.getelementbyid("img1").src = path;
    }
   }
  }

html代码,其实不重要啦 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<div class = "form-group has-success has-feedback" >
        <label for = "info_photo" class = "col-sm-2 control-label" >物品图片</label>
        <div class = "col-sm-10" >
 
         <span class = "btn btn-success btn-file" > 选择图片 <span
          class = "glyphicon glyphicon-picture" aria-hidden= "true" ></span>
          <input type= "file" name= "info_photo" value= "" id= "info_photo"
          onchange= 'previewimage(this)' />
         </span>
        </div>
 
       </div>
 
       <div class = "form-group has-success has-feedback" >
        <label for = "info_desc" class = "col-sm-2 control-label" ><span
         style= "color: red;" >* </span> 详细描述</label>
        <div class = "col-sm-10" >
         <textarea class = "form-control" rows= "5" id= "info_desc"
          name= "info_desc" placeholder= "如需补充,请填写..." title= "可包含中文数字和常用字符" ></textarea>
         <div id= "photo_info" class = "photo_info" ></div>
        </div>
       </div>

css代码就不上了啊 顺便补充一下thinkphp上传图片代码,把整个发布消息都拷过了算了 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public function losething(){
    $m =m( 'info' );
    $m ->create();
    $upload = new \think\upload(); // 实例化上传类
    $upload ->maxsize =  3145728 ; // 设置附件上传大小
    $upload ->exts  =  array ( 'jpg' , 'gif' , 'png' , 'jpeg' , 'bmp' ); // 设置附件上传类型
    $upload ->rootpath = './public/' ;
    $upload ->savepath = '/uploads/' ; // 设置附件上传目录
    $upload ->autosub = false;
    // 上传文件
    $info = $upload ->upload();
    if ( $info ) {
   // 上传成功
    foreach ( $info as $file ){
     $m ->info_photo= $file [ 'savename' ];
    }
   }
    $m ->create_time= date ( 'y-m-d h:i:s' ,time());
    $m ->uid= $_session [ 'id' ];
    $m ->username= $_session [ 'username' ];
   /* $m->user_img=$_session['filename']; */
 
    $m ->info_type= "寻物启事" ; //信息类型为失物
    $lastid = $m ->add();
    if ( $lastid ){
     $this ->success( '发布成功' );
    } else {
     $this ->error( '发布失败,请先登录' );
    }
   }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

最后此篇关于thinkphp jquery实现图片上传和预览效果的文章就讲到这里了,如果你想了解更多关于thinkphp jquery实现图片上传和预览效果的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com