Home  >  Article  >  WeChat Applet  >  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

jacklove
jackloveOriginal
2018-06-11 17:34:2812892browse

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>


##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"];  
	    }   
?>

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>

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:
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