Browse Source

修改打开文件的接口

DIng 5 tháng trước cách đây
mục cha
commit
c72ce299f2
1 tập tin đã thay đổi với 18 bổ sung40 xóa
  1. 18 40
      pages/Visualizatio/Visualizatio.js

+ 18 - 40
pages/Visualizatio/Visualizatio.js

@@ -192,8 +192,8 @@ Page({
     });
   },  
 
-  // 导出数据按钮点击事件
-onExport: function () {
+ // 导出数据按钮点击事件
+ onExport: function () {
   const tableName = this.data.tableName;
   const fileFormat = this.data.fileFormat;
 
@@ -202,57 +202,35 @@ onExport: function () {
   });
 
   // 向后端发送请求,获取表格数据并导出
-  wx.request({
-    url: `https://soilgd.com:5000/export_data?table=${tableName}&format=${fileFormat}`,
-    method: 'GET',
-    responseType: 'arraybuffer',  // 处理二进制文件
+  const downloadUrl = `https://soilgd.com:5000/export_data?table=${tableName}&format=${fileFormat}`;
+
+  wx.downloadFile({
+    url: downloadUrl,
     success: (res) => {
       wx.hideLoading();
-
-      // 确保响应体返回的数据是有效的文件
       if (res.statusCode === 200) {
-        const fileName = `${tableName}_data.${fileFormat === 'excel' ? 'xlsx' : 'csv'}`;
-        const filePath = wx.env.USER_DATA_PATH + '/' + fileName;
-
-        // 保存文件到本地
-        wx.getFileSystemManager().writeFile({
-          filePath: filePath,
-          data: res.data,
-          encoding: 'binary',
+        // 文件下载成功后,尝试打开文件
+        wx.openDocument({
+          filePath: res.tempFilePath, // 使用临时文件路径
+          fileType: fileFormat === 'excel' ? 'xlsx' : 'xls',
           success: () => {
-            // 打开文件
-wx.openDocument({
-  filePath: filePath,
-  fileType: fileFormat === 'excel' ? 'xlsx' : 'csv',
-  success: () => {
-    console.log('文件打开成功');
-  },
-  fail: (error) => {
-    console.error('文件打开失败', error);
-    wx.showToast({
-      title: '打开文件失败,文件类型不支持或微信版本过低',
-      icon: 'none'
-    });
-  }
-});
-
-            // 在文件数据右上角显示菜单
-            this.setData({
-              showMenu: true,  // 显示右上角菜单
-              filePath: filePath  // 保存文件路径
+            console.log('文件打开成功');
+            wx.showToast({
+              title: '文件已打开',
+              icon: 'success'
             });
           },
-          fail: (err) => {
+          fail: (error) => {
+            console.error('文件打开失败', error);
             wx.showToast({
-              title: '文件保存失败',
+              title: '打开文件失败',
               icon: 'none'
             });
-            console.error('文件保存失败', err);
           }
         });
       } else {
         wx.showToast({
-          title: '导出失败,请重试',
+          title: '导出失败,请检查网络或重试',
           icon: 'none'
         });
       }