目前小程序沒(méi)有能實(shí)現(xiàn)此功能的 API 所以我這里通過(guò)使用 web-view 實(shí)現(xiàn);
實(shí)現(xiàn)流程:
1. 在小程序后臺(tái)配置業(yè)務(wù)域名
2. 在服務(wù)器寫(xiě)一個(gè)html,實(shí)現(xiàn)表單上傳文件
3.后端php接收文件并存到一個(gè)服務(wù)器文件夾,把文件名存到數(shù)據(jù)庫(kù)以后檢索用
4.在微信小程序創(chuàng)建一個(gè)頁(yè)面,里面使用web-view達(dá)到上傳文件的目的;
效果圖:
具體實(shí)現(xiàn):
1. 在小程序后臺(tái)配置業(yè)務(wù)域名
成品ppt在線(xiàn)生成,百種模板可供選擇?????點(diǎn)擊使用;
地址:https://mp.weixin.qq.com/wxopen/appdatacount
2. 在服務(wù)器寫(xiě)一個(gè)html,實(shí)現(xiàn)表單上傳文件
index.html文件
<!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> <!-- 其實(shí)我們可以把iframe標(biāo)簽隱藏掉 --> <script type="text/javascript"> function upload() { $("#form1").submit(); var t = setInterval(function() { //獲取iframe標(biāo)簽里body元素里的文字。即服務(wù)器響應(yīng)過(guò)來(lái)的"上傳成功"或"上傳失敗" var word = $("iframe[name='frame1']").contents().find("body").text(); if(word != "") { // alert(word); //彈窗提示是否上傳成功 // clearInterval(t); //清除定時(shí)器 } }, 1000); } </script> </body> </html>
3.后端php接收文件并存到一個(gè)服務(wù)器文件夾,把文件名存到數(shù)據(jù)庫(kù)以后檢索用
up_file.php 文件:
<?php header("Content-Type:text/html;charset=utf8"); header("Access-Control-Allow-Origin: *"); //解決跨域 header('Access-Control-Allow-Methods:POST');// 響應(yīng)類(lèi)型 header('Access-Control-Allow-Headers:*'); // 響應(yīng)頭設(shè)置 $link=mysql_connect("localhost","root","root"); mysql_select_db("new_test", $link); //選擇數(shù)據(jù)庫(kù) mysql_query("SET NAMES utf8");//解決中文亂碼問(wèn)題 error_reporting(0); if ($_FILES["file"]["error"] > 0) { echo "錯(cuò)誤: " . $_FILES["file"]["error"] . "<br />"; } else { $dlog["name"]=$_FILES["file"]["name"]; $dlogs=$dlog; //echo urldecode(json_encode($dlogs)); $name =$_FILES["file"]["name"]; echo '上傳成功!'; echo $name; //插入數(shù)據(jù)到數(shù)據(jù)庫(kù) $strsql = "insert into name (fileName) values('$name')"; //mysql_query() 函數(shù)執(zhí)行一條 MySQL 查詢(xún)。SELECT,SHOW,EXPLAIN 或 DESCRIBE 都需要用這個(gè)函數(shù)執(zhí)行 $result = @mysql_query($strsql); // echo "文件名: " . $_FILES["file"]["name"] . "<br />"; // echo "類(lèi)型: " . $_FILES["file"]["type"] . "<br />"; // echo "大小: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; } if (file_exists("upload/" . $_FILES["file"]["name"])) { // echo $_FILES["file"]["name"] . " 文件已經(jīng)存在. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); // echo "文件已經(jīng)被存儲(chǔ)到: " . "upload/" . $_FILES["file"]["name"]; } ?>
4.在微信小程序創(chuàng)建一個(gè)頁(yè)面,里面使用web-view達(dá)到上傳文件的目的;
web.wxml文件
<!--pages/web/web.wxml--> <web-view src='https://dwb.lynncain.cn/H5/'></web-view>
注:微信小程序web-view標(biāo)簽使用如上,無(wú)需多余代碼。
本文詳解了關(guān)于微信小程序上傳word、txt、Excel、PPT等文件的內(nèi)容,更多相關(guān)內(nèi)容請(qǐng)關(guān)注php中文網(wǎng)。
相關(guān)推薦:
講解php 支持?jǐn)帱c(diǎn)續(xù)傳的文件下載類(lèi)的相關(guān)內(nèi)容
如何通過(guò)php 過(guò)濾html標(biāo)記屬性類(lèi)
以上就是關(guān)于微信小程序上傳word、txt、Excel、PPT等文件的詳解的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
不想找模板、不想調(diào)排版?試試這款智能PPT生成工具,輸入主題,幾分鐘生成一套高質(zhì)量PPT,省時(shí)又省心。
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://m.miracleart.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)