我在Vuejs中使用vuetify组件作为前端。我想创建一个带文件上传的用户注册表单。我可以在vuetify中使用nightmare.on('download', function(state, downloadItem) {
if(state == 'started') {
nightmare.emit('download', 'yourfilename.txt', downloadItem);
} else if(state == 'completed') {
console.log('Donwload completed');
} else if(state == 'interrupted') {
endNightmare();
} else if(state == 'cancelled') {
endNightmare();
}
});
nightmare
.downloadManager()
.goto(url)
.click('#javascriptDownloadBtn')
.waitDownloadsComplete()
.then(function() {
return nightmare.end();
})
创建表单,但是如何上传文件。使用哪个组件或任何其他替代方式。
答案 0 :(得分:46)
Vue JS直到今天还没有文件输入功能,因此您可以调整v-text-field以像图像输入字段一样工作。概念是,创建一个文件输入字段然后使用css隐藏它,并在v-text-field中添加一个事件来触发该特定文件输入字段以上传图像。我已经附上了片段请玩那个,我也有一个使用vue和vuetify创建的小提琴,请访问here。谢谢!
new Vue({
el: '#app',
data: () => ({
title: "Image Upload",
dialog: false,
imageName: '',
imageUrl: '',
imageFile: ''
}),
methods: {
pickFile () {
this.$refs.image.click ()
},
onFilePicked (e) {
const files = e.target.files
if(files[0] !== undefined) {
this.imageName = files[0].name
if(this.imageName.lastIndexOf('.') <= 0) {
return
}
const fr = new FileReader ()
fr.readAsDataURL(files[0])
fr.addEventListener('load', () => {
this.imageUrl = fr.result
this.imageFile = files[0] // this is an image file that can be sent to server...
})
} else {
this.imageName = ''
this.imageFile = ''
this.imageUrl = ''
}
}
}
})
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet">
<link href="https://unpkg.com/vuetify/dist/vuetify.min.css" rel="stylesheet">
<div id="app">
<v-app>
<v-toolbar dark color="primary">
<v-toolbar-side-icon></v-toolbar-side-icon>
<v-toolbar-title class="white--text">{{ title }}</v-toolbar-title>
<v-spacer></v-spacer>
<v-btn icon @click="dialog = !dialog">
<v-icon>link</v-icon>
</v-btn>
</v-toolbar>
<v-content>
<v-container fluid>
<v-flex xs12 class="text-xs-center text-sm-center text-md-center text-lg-center">
<img :src="imageUrl" height="150" v-if="imageUrl"/>
<v-text-field label="Select Image" @click='pickFile' v-model='imageName' prepend-icon='attach_file'></v-text-field>
<input
type="file"
style="display: none"
ref="image"
accept="image/*"
@change="onFilePicked"
>
</v-flex>
<v-dialog v-model="dialog" max-width="290">
<v-card>
<v-card-title class="headline">Hello World!</v-card-title>
<v-card-text>Image Upload Script in VUE JS
<hr>Yubaraj Shrestha
<br>http://yubarajshrestha.com.np/</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn color="green darken-1" flat="flat" @click.native="dialog = false">Close</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</v-container>
</v-content>
</v-app>
</div>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vuetify/dist/vuetify.js"></script>
答案 1 :(得分:27)
这是我们将来会添加的内容,但目前尚未添加。有关github的讨论,其中有几个用户发布了他们暂时使用的实现,https://github.com/vuetifyjs/vuetify/issues/238
答案 2 :(得分:16)
一个简单的技巧是:
<v-btn color="success" @click="$refs.inputUpload.click()">Success</v-btn>
<input v-show="false" ref="inputUpload" type="file" @change="yourFunction" >
看起来像个玩笑,但有效。
只需输入以下属性
然后创建一个按钮,当您单击它时,单击输入的上载按钮
答案 3 :(得分:0)
好消息。
Vuetify中提供了从版本2.0.0.-beta.8 v-file-input
开始的版本。您应该像这样使用它:
<template>
<v-file-input accept=".txt" label="Select File..."></v-file-input>
</template>