vue前端获取项目下的静态资源文件夹中的文件并下载

1.图片

前端项目/public/static/image文件夹下,两张图片,因为不会经常改变所以做成静态资源
在这里插入图片描述
从项目中获取这两张图片

//require.context(检索目录、是否检索子文件、正则表达式匹配的)
const requireContext = require.context('../../../public/static/image/', false, /^\.\/.*$/) 
requireContext.keys().forEach(key=>{
	// console.log(key)    // 输出./image.jpg
	let file = key.substring(2)
	this.imageList.push('../../../static/image/'+file)
})

html:

<!--展示轮播图-->
<van-swipe class="swipe" autoplay="3000">
	<van-swipe-item v-for="(item,index) in imageList" :key="index" :title="item.name">
		<van-image width="100%" height="5rem" :src="item" :alt="item"/>
	</van-swipe-item>
</van-swipe>

2.文件

前端项目的 \public\static\faultFile文件夹中放入模板文件:
在这里插入图片描述

实现效果:

在这里插入图片描述
代码:

前端下载使用< a >标签的自带的download下载

<el-dialog title="下载模板" :visible.sync="showDialog" width="600px" @close="close">
	<transition-group class="upload-file-list el-upload-list el-upload-list--text" name="el-fade-in-linear" tag="ul" v-show="fileList.length>0">
		<li class="el-upload-list__item ele-upload-list__item-content" v-for="(item,index) in fileList" :key="index">
			<el-link :underline="false" target="_blank">
				<a class="el-icon-download" style="color:#1890ff;margin-left: 7px;" :href="process+'/static/faultFile/'+item" :download="item">{{item}}</a>
			</el-link>
		</li>
	</transition-group>
</el-dialog>

......js部门在下面

<style scoped>
.upload-file-uploader {
  margin-bottom: 5px;
}

.upload-file-list .el-upload-list__item {
  border: 1px solid #e4e7ed;
  line-height: 2;
  margin-bottom: 10px;
  position: relative;
}

.upload-file-list .ele-upload-list__item-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: inherit;
}

.ele-upload-list__item-content-action .el-link {
  margin-right: 10px;
}
</style>
export default {
  name: "DownTemplate",
  data(){
    return{
      //这个是部署前端包时的包名,访问静态文件的时候需要加上包名(和vue.config.js里的publicPath保持一致)
      process: process.env.VUE_APP_NAME,  
      showDialog: false,
      fileList: [],
    }
  },
  methods:{
  	//打开下载模板dialog
    open() {
      this.showDialog = true
      //获取文件夹中的文件名称,放进fileList中
      const files = require.context("../../../../../public/static/faultFile/", true, /\.doc$/); //只获取文件夹中.doc结尾的文件
      files.keys().forEach(item=>{
        this.fileList.push(item.substring(2))
      })
       
    },
    close(){
      this.fileList = []
    },
  },
}

参考文章:require.context()的用法详解

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Django 项目,你可以使用 Vue.js 创建前端文件。下面是一些简单的步骤: 1. 安装 Vue CLI 首先,你需要安装 Vue CLI。你可以使用以下命令安装: ``` npm install -g @vue/cli ``` 2. 创建 Vue.js 项目 在 Django 项目的根目录下创建一个新的文件夹,比如 `frontend`,然后在该文件夹运行以下命令来创建一个新的 Vue.js 项目: ``` vue create . ``` 这将创建一个新的 Vue.js 项目,并将其添加到当前文件夹。你需要选择一些选项,如 Babel、CSS 预处理器等,根据你的需求进行选择。 3. 配置 Django 项目 接下来,你需要将 Vue.js 项目与 Django 项目连接起来。可以在 Django 项目的 `settings.py` 文件添加以下配置: ```python STATICFILES_DIRS = [ os.path.join(BASE_DIR, "frontend", "dist", "static"), ] TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", "DIRS": [ os.path.join(BASE_DIR, "frontend", "dist"), ], "APP_DIRS": True, "OPTIONS": { "context_processors": [ "django.template.context_processors.debug", "django.template.context_processors.request", "django.contrib.auth.context_processors.auth", "django.contrib.messages.context_processors.messages", ], }, }, ] ``` 这将使 Django 项目在运行时可以找到 Vue.js 生成的静态文件和模板。 4. 构建 Vue.js 项目 最后,你需要构建 Vue.js 项目并将生成的静态文件放入 Django 项目。可以在 Vue.js 项目的根目录下运行以下命令来构建项目: ``` npm run build ``` 这将生成一个名为 `dist` 的文件夹,其包含静态文件和模板。你需要将该文件夹复制到 Django 项目的根目录下。 现在,你已经成功地将 Vue.js 项目添加到 Django 项目。在 Django 项目使用 Vue.js 时,你可以在模板使用 Vue.js 组件,或者使用 Django REST framework 创建 API 并从 Vue.js 组件获取数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值