Home WeChat Applet WeChat Development Detailed explanation on uploading word, txt, Excel, PPT and other files to WeChat mini program

Detailed explanation on uploading word, txt, Excel, PPT and other files to WeChat mini program

Jun 11, 2018 pm 05:34 PM

Currently, the mini program does not have an API that can implement this function, so I implement it here by using web-view;

Implementation process:

1. Configure the business domain name in the background of the mini program

2. Write an html on the server to implement the form upload file

3. The back-end php receives the file and saves it to a server folder, and saves the file name to the database for later retrieval

4. Create a page in the WeChat applet and use web-view to upload files;

Rendering:

Detailed implementation:

1. Configure the business domain name in the mini program background

Address: https://mp.weixin.qq.com/wxopen/appdatacount


2. Write an html on the server to implement the form upload file

index.html file

<!DOCTYPE html>
<html>

	<head>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
		<meta charset="UTF-8">
		<title>Title</title>
		<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.js"></script>
	</head>

	<body>
		<form id="form1" action="https://dwb.lynncain.cn/H5/up_file.php" target="frame1" method="post" enctype="multipart/form-data">
			<input type="file" name="file">
			<input type="button" value="上传" onclick="upload()">
		</form>
		<iframe name="frame1" frameborder="0" height="40"></iframe>
		<!-- 其实我们可以把iframe标签隐藏掉 -->
		<script type="text/javascript">
			function upload() {
				$("#form1").submit();
				var t = setInterval(function() {
					//获取iframe标签里body元素里的文字。即服务器响应过来的"上传成功"或"上传失败"
					var word = $("iframe[name=&#39;frame1&#39;]").contents().find("body").text();
					if(word != "") {
//						alert(word); //弹窗提示是否上传成功
//						clearInterval(t); //清除定时器
					}
				}, 1000);
			}
		</script>
	</body>

</html>
Copy after login


##3. The back-end php receives the file and saves it to a server folder, and saves the file name to the database for later retrieval using

up_file.php file:


<?php  
    header("Content-Type:text/html;charset=utf8"); 
	header("Access-Control-Allow-Origin: *"); //解决跨域
	header(&#39;Access-Control-Allow-Methods:POST&#39;);// 响应类型  
	header(&#39;Access-Control-Allow-Headers:*&#39;); // 响应头设置 
    $link=mysql_connect("localhost","root","root"); 
    mysql_select_db("new_test", $link); //选择数据库
    mysql_query("SET NAMES utf8");//解决中文乱码问题
	error_reporting(0);
	if ($_FILES["file"]["error"] > 0)  
	  {  
	  echo "错误: " . $_FILES["file"]["error"] . "<br />";  
	  }  
	else  
	  {  
		$dlog["name"]=$_FILES["file"]["name"];
  		$dlogs=$dlog;	
    	//echo urldecode(json_encode($dlogs));
		$name =$_FILES["file"]["name"];
		echo &#39;上传成功!&#39;;
		echo $name;
			//插入数据到数据库 
			$strsql = "insert into name (fileName) values(&#39;$name&#39;)";
			//mysql_query() 函数执行一条 MySQL 查询。SELECT,SHOW,EXPLAIN 或 DESCRIBE 都需要用这个函数执行
			$result = @mysql_query($strsql);
//	  echo "文件名: " . $_FILES["file"]["name"] . "<br />";  
//	  echo "类型: " . $_FILES["file"]["type"] . "<br />";  
//	  echo "大小: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";   
	  }  
	 if (file_exists("upload/" . $_FILES["file"]["name"]))  
	    {  
//	      echo $_FILES["file"]["name"] . " 文件已经存在. ";  
	    }  
	else  
	    {  
	      move_uploaded_file($_FILES["file"]["tmp_name"],  
	      "upload/" . $_FILES["file"]["name"]);  
//	      echo "文件已经被存储到: " . "upload/" . $_FILES["file"]["name"];  
	    }   
?>
Copy after login

4. Create a WeChat applet page, which uses web-view to upload files;

web.wxml file

<!--pages/web/web.wxml-->
 <web-view src=&#39;https://dwb.lynncain.cn/H5/&#39;></web-view>
Copy after login
Note: The WeChat applet web-view tag is used as above, no redundant code is required.

This article explains in detail the content of WeChat applet uploading word, txt, Excel, PPT and other files. For more related content, please pay attention to the php Chinese website.

Related recommendations:

Introduction to bubbling, dichotomy insertion, quick sort algorithm

Explain how PHP supports breaking Related content of the file download class that you click on to resume the upload

How to filter the html tag attribute class through php

The above is the detailed content of Detailed explanation on uploading word, txt, Excel, PPT and other files to WeChat mini program. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)