吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 374|回复: 12
收起左侧

[求助] 求助代码修改

[复制链接]
doudouxiao 发表于 2025-1-18 11:27
本帖最后由 doudouxiao 于 2025-1-18 21:05 编辑

使用vue3,写的代码想实现输入服务器地址将服务器文件下载到手机Download文件夹下,但是点击下载提示下载失败,求助一位大佬,谢谢!在线等,可以悬赏!
[Asm] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<template>
  <view class="content">
    <image class="logo" src="/static/logo.png"></image>
    <view class="text-area">
      <text class="title">{{ title }}</text>
    </view>
    <view class="input-area">
      <input type="text" v-model="serverAddress" placeholder="请输入服务器地址" />
    </view>
    <view class="select-area">
      <picker mode="selector" :range="departments" @change="onDepartmentChange">
        <view class="picker">
          当前选择:{{ departments[selectedDepartment] }}
        </view>
      </picker>
    </view>
    <button class="download-button" @click="downloadFiles">资料更新</button>
  </view>
</template>
 
<script>
export default {
  data() {
    return {
      title: '资料更新',
      serverAddress: '',
      departments: ['部门1', '部门2', '部门3'],
      selectedDepartment: 0
    };
  },
  methods: {
    onDepartmentChange(event) {
      this.selectedDepartment = event.detail.value;
    },
    downloadFiles() {
      if (!this.serverAddress) {
        uni.showToast({
          title: '请输入服务器地址',
          icon: 'none'
        });
        return;
      }
 
      const department = this.departments[this.selectedDepartment];
      const encodedDepartment = encodeURIComponent(department + '/files.zip');
      const url = `${this.serverAddress}/download.php?file=${encodedDepartment}`;
 
      console.log('Download URL:', url); // 打印下载 URL,用于调试
 
      uni.downloadFile({
        url: url,
        success: (res) => {
          if (res.statusCode === 200) {
            const tempFilePath = res.tempFilePath;
 
            // 保存文件到设备的 Download 文件夹
            const savePath = `${wx.env.USER_DATA_PATH}/Download/${department}_files.zip`;
 
            wx.getFileSystemManager().saveFile({
              tempFilePath: tempFilePath,
              filePath: savePath,
              success: (result) => {
                uni.showToast({
                  title: '下载成功',
                  icon: 'success'
                });
              },
              fail: (err) => {
                console.log('保存文件失败:', err);
                uni.showToast({
                  title: '保存文件失败',
                  icon: 'none'
                });
              }
            });
          } else {
            console.log('Download failed with status code:', res.statusCode);
            uni.showToast({
              title: `下载失败,状态码:${res.statusCode}`,
              icon: 'none'
            });
          }
        },
        fail: (err) => {
          console.log('Download failed:', err);
          uni.showToast({
            title: '下载失败',
            icon: 'none'
          });
        }
      });
    }
  }
};
</script>
 
<style scoped>
.content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20rpx;
  background-color: #f5f5f5;
  min-height: 100vh;
}
 
.logo {
  height: 200rpx;
  width: 200rpx;
  margin-top: 50rpx;
}
 
.text-area {
  display: flex;
  justify-content: center;
  margin-top: 20rpx;
}
 
.title {
  font-size: 48rpx;
  color: #333;
  font-weight: bold;
}
 
.input-area {
  width: 80%;
  margin: 20rpx 0;
}
 
input {
  width: 100%;
  padding: 20rpx;
  font-size: 32rpx;
  border: 1px solid #ccc;
  border-radius: 10rpx;
}
 
.select-area {
  width: 80%;
  margin: 20rpx 0;
}
 
.picker {
  width: 100%;
  padding: 20rpx;
  font-size: 32rpx;
  background-color: #fff;
  border: 1px solid #ccc;
  border-radius: 10rpx;
  text-align: center;
}
 
.download-button {
  width: 80%;
  padding: 20rpx;
  font-size: 36rpx;
  color: #fff;
  background-color: #007aff;
  border: none;
  border-radius: 10rpx;
  text-align: center;
  margin-top: 20rpx;
}
</style>

index.txt

3.71 KB, 下载次数: 3, 下载积分: 吾爱币 -1 CB

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

wonder2018 发表于 2025-1-18 21:03
代码不是很长的话直接插到帖子里面啊,为什么还要人下载嘞。
 楼主| doudouxiao 发表于 2025-1-18 21:04
wonder2018 发表于 2025-1-18 21:03
代码不是很长的话直接插到帖子里面啊,为什么还要人下载嘞。

不好意思,以后知道了,感谢
wonder2018 发表于 2025-1-18 23:21
问题原因可能是跨域了,如果服务端没有配置允许跨域。
具体错误需要查看第85行的输出,如果无法打开控制台,可以用alert或者把error.message绑定到页面的一个div上面看
可以在服务端为相关接口添加响应头 Access-Control-Allow-Origin:*
如果接口是别人的,你改不了,可以在你自己的服务器上面配置反向代{过}{滤}理,代{过}{滤}理到原始接口。
Laotu 发表于 2025-1-19 01:45
nodejs呀,我写了一个,很方便
 楼主| doudouxiao 发表于 2025-1-19 09:39
Laotu 发表于 2025-1-19 01:45
nodejs呀,我写了一个,很方便

大佬,可以分享一下嘛?谢谢,麻烦您了
 楼主| doudouxiao 发表于 2025-1-19 10:07
wonder2018 发表于 2025-1-18 23:21
问题原因可能是跨域了,如果服务端没有配置允许跨域。
具体错误需要查看第85行的输出,如果无法打开控制台 ...

就是跨域了,跨域不知道怎么搞,服务器是自己的,我写了一个html测试可以正常下载,但是手机app就是不行报错了
wonder2018 发表于 2025-1-19 13:38
如果输入框里面输入的服务器地址,是你自己的服务器,就找到那个服务器上面的接口配置位置。
nginx部署的话就在接口的location块中添加一行-> add_header Access-Control-Allow-Origin "*";
如果是其它的部署方式(比如java,node等等),就在搜索引擎搜对应的部署方式如何添加响应头,然后添加Access-Control-Allow-Origin将值设为星号就行了。

注意:这里要修改的服务器是你下载资源的服务器,不是你部署程序的地址。
 楼主| doudouxiao 发表于 2025-1-19 13:47
wonder2018 发表于 2025-1-19 13:38
如果输入框里面输入的服务器地址,是你自己的服务器,就找到那个服务器上面的接口配置位置。
nginx部署的 ...

老师,求助一下您,方便的话看一下私信
Laotu 发表于 2025-1-20 10:01
doudouxiao 发表于 2025-1-19 09:39
大佬,可以分享一下嘛?谢谢,麻烦您了

我写的只是简单的图片分享,
用户无限上传图片,其他人可以批量下咋至手机相册。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-6-14 11:45

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表