100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 小程序图片上传与回显(包括前后端)

小程序图片上传与回显(包括前后端)

时间:2023-06-11 22:24:58

相关推荐

小程序图片上传与回显(包括前后端)

前台:

js:

var app = getApp()var total = [];Page({data: {},chooseImg: function () {var _this = this;wx.chooseImage({count: 9, // 默认9sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有success: function (res) {// 返回选定照片的本地文件路径列表,tempFilePath可以作为img标签的src属性显示图片var tempFilePaths = res.tempFilePaths;console.log(res)_this.setData({img_l: res.tempFilePaths})console.info(res.tempFilePaths.length);}})},loadimg: function () {var _this = this;wx.uploadFile({url: 'http://127.0.0.1/xiaoyuan/goods/upload.php', //接口filePath: _this.data.img_l[0],name: 'file',formData: {'user': 'test'},success: function (res) {var data = res.data;console.log(data);},fail: function (error) {console.log(error);}})}})

wxml:

<button bindtap="chooseImg" style='width:80%'>请添加商品图片</button><image style='width: 400px;height: 400px;border:1px solid #ccc;margin: 5px;' src='{{img_l}}'/><button bindtap='loadimg' style='width:80%; background-color: rgb(90, 199, 69);'>上传</button>

后台(php):

upload.php:

<?phpdate_default_timezone_set(PRC); //设置时区$code = $_FILES['file'];//获取小程序传来的图片 $uploaded_file=$_FILES['file']['tmp_name']; $user_path=$_SERVER['DOCUMENT_ROOT']."/xiaoyuan/"."/goods/"."img"; //放到服务器下指定的文件夹$move_to_file=$user_path."/".$_FILES['file']['name']; $file_true_name=$_FILES['file']['name']; echo $file_true_name;echo $move_to_file;move_uploaded_file($uploaded_file,iconv("utf-8","gb2312",$move_to_file));?>

java:

import mons.fileupload.FileItem;import mons.fileupload.FileUploadException;import mons.fileupload.disk.DiskFileItemFactory;import mons.fileupload.servlet.ServletFileUpload;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.*;import java.util.List;public class Uploadimage extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doPost(req,resp);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request.setCharacterEncoding("utf-8"); //设置编码DiskFileItemFactory factory = new DiskFileItemFactory();String str = request.getSession().getServletContext().getRealPath("");String path = request.getRealPath("/upload");String pathStr=null;System.err.println("上传的图片路径:"+path);factory.setRepository(new File(path));factory.setSizeThreshold(1024*1024) ;ServletFileUpload upload = new ServletFileUpload(factory);try {//可以上传多个文件@SuppressWarnings("unchecked")List<FileItem> list = (List<FileItem>)upload.parseRequest(request);for(FileItem item : list){String name = item.getFieldName();if(item.isFormField()){}else {//获取路径名System.out.println("item.getFieldName()是"+item.getFieldName());String value = item.getName() ;System.out.println("item.getName()是"+item.getName());String filename= value;System.out.println("filename----->"+filename);str+="/xiaoyuan/"+"/goods/"+"/img/"+filename;System.out.println("文件存储的路径:"+str);pathStr=filename;File file=new File(str);OutputStream out = new FileOutputStream(file);InputStream in = item.getInputStream() ;int length = 0 ;byte [] buf = new byte[1024] ;System.out.println("获取上传文件的总共的容量:"+item.getSize());while( (length = in.read(buf) ) != -1){out.write(buf, 0, length);}in.close();out.close();}}} catch (FileUploadException e) {e.printStackTrace();}catch (Exception e) {}PrintWriter printWriter=response.getWriter();// printWriter.print("{"path":""+pathStr+""}");System.out.println("pathStr----->"+pathStr);printWriter.write(pathStr);printWriter.print(pathStr);printWriter.flush();printWriter.close();}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。