小编Ash*_*y B的帖子

在vue.js中更改文件输入

使用纯HTML/JS,可以查看输入元素的所选文件的JavaScript File对象,如下所示:

<input type="file" id="input" multiple onchange="handleFiles(this.files)">
Run Code Online (Sandbox Code Playgroud)

但是,当将其转换为'Vue'方式时,它似乎不会按预期工作,只返回undefined而不是返回File对象数组.

这是它在我的Vue模板中的样子:

<input type="file" id="file" class="custom-file-input" 
  v-on:change="previewFiles(this.files)" multiple>
Run Code Online (Sandbox Code Playgroud)

previewFiles函数就是(位于方法)的情况如下:

  methods: {
    previewFiles: function(files) {
      console.log(files)
    }
  }
Run Code Online (Sandbox Code Playgroud)

这样做有替代/正确的方法吗?谢谢

javascript vue.js vue-component vuejs2

38
推荐指数
4
解决办法
4万
查看次数

使用程序化导航Vue.js传递道具

我有一个Vue组件,其中有一个名为'title'的道具,例如:

<script>
export default {
  props: ['title'],
  data() {
    return {
    }
  }
}
</script>
Run Code Online (Sandbox Code Playgroud)

在某个操作完成后,我以编程方式导航到组件.有没有办法以编程方式路由用户,同时还设置道具值?我知道你可以创建这样的链接:

<router-link to="/foo" title="example title">link</router-link>
Run Code Online (Sandbox Code Playgroud)

但是,有没有办法做以下事情?

this.$router.push({ path: '/foo', title: 'test title' })
Run Code Online (Sandbox Code Playgroud)

编辑:

如我所知,我已将路线更改为以下内容:

   {
      path: '/i/:imageID',
      component: Image,
      props: true
    }
Run Code Online (Sandbox Code Playgroud)

并导航到以下内容:

this.$router.push({ path: '/i/15', params: {title: 'test title' }})
Run Code Online (Sandbox Code Playgroud)

但是,我的图像组件(模板 - 见下文)仍然没有显示任何标题.

<h1>{{ title}}</h1>
Run Code Online (Sandbox Code Playgroud)

有什么可能导致问题吗?

javascript vue.js vue-router vuejs2

25
推荐指数
2
解决办法
2万
查看次数

将缓冲区上传到谷歌云存储

我正在尝试将缓存(从表单上传的文件)保存到Google云端存储,但似乎Google Node SDK只允许上传具有给定路径的文件(读/写流).

这是我用于AWS(S3)的东西 - Google节点SDK中的其他类似内容吗?:

var fileContents = new Buffer('buffer');

var params = {
  Bucket: //bucket name
  Key: //file name
  ContentType: // Set mimetype
  Body: fileContents 
};

s3.putObject(params, function(err, data) {
// Do something 
});
Run Code Online (Sandbox Code Playgroud)

我到目前为止找到的唯一方法是将缓冲区写入磁盘,使用SDK上传文件(指定新文件的路径),然后在文件成功上传后删除该文件 - 这样做的缺点是整个过程明显变慢,使用谷歌存储似乎是不可行的.是否有任何工作/方式上传缓冲区?

google-app-engine node.js gcloud gcloud-node

13
推荐指数
5
解决办法
6228
查看次数

Express:req.flash()需要会话

我在使用connect-flash工作时遇到了一些问题,我得到的错误信息是:

'错误:req.flash()需要会话'

我已经看到这可能是因为应用程序的排序,但我不确定这是否是这种情况.

App.js:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var flash = require('connect-flash');
var passport = require('passport');
var app = express();

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(session({secret: '{secret}', name: 'session_id', saveUninitialized: true, resave: true}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
Run Code Online (Sandbox Code Playgroud)

路线等如下

闪存生成代码:

passport.authenticate('local', { failureRedirect: '/login', successRedirect: '/', failureFlash: true})
Run Code Online (Sandbox Code Playgroud)

node.js express passport-local passport.js connect-flash

8
推荐指数
2
解决办法
1万
查看次数

Multer - 在 fileFilter 期间获取基础文件的内容

我正在尝试确保上传的文件是图像,同时使用 multer 处理文件上传。

fileFilter函数采用以下形式:

  var fileType = require('file-type');

  fileFilter: function(req, file, cb){
    // Check filetype based on file content
    var filetype = fileType(// File buffer here);
    // Callback - file OK
    cb(null, true)
  }
Run Code Online (Sandbox Code Playgroud)

问题file仅包含以下内容:

  { fieldname: 'photo',
  originalname: 'photo.png',
  encoding: '7bit',
  mimetype: 'image/png' }
Run Code Online (Sandbox Code Playgroud)

有没有办法在 fileFilter 函数中实际获取文件的内容(缓冲区),以便我可以检查文件的内容以确定其类型?

编辑:如果这可以使用busboy(模块multer使用)来完成,我愿意接受涉及该问题的解决方案。

node.js express multer busboy

7
推荐指数
0
解决办法
1093
查看次数

在vue.js模板中包含外部脚本

我是Vue.js和web-pack的新手,所以我决定使用vue-cli(webpack)来构建一个初始应用程序.我正在尝试将一个外部脚本(例如<script src="...")包含在一个全局不需要的模板中(对于每个页面/组件),但是Vue警告不允许这样做.

我的index.html文件类似于最初生成的文件:

<html lang="en">

<head>
  <title>App</title>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- Bootstrap CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">

</head>

<body>
  <div id="app"></div>

  <!-- jQuery first, then Tether, then Bootstrap JS. -->
  <script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

我的App.vue也类似于生成的:

<template>
<div id="app">

  <div class="container pt-5">
    <router-view></router-view>
  </div>

</div>
</template>
Run Code Online (Sandbox Code Playgroud)

我有一个路由到/upload我的路由文件,它映射到需要dropzone.js(外部脚本)的上传组件.我可以将它包含在我的index.html中,类似于如何加载引导程序,但是当只有这个组件需要它时,为每个页面/组件加载它似乎不太理想.

但是,如上所述,我只是将它包含在我的模板文件中:

<template>
<div>
  <h2>Upload Images</h2>
  <form action="/file-upload" class="dropzone">
    <div …
Run Code Online (Sandbox Code Playgroud)

javascript vue.js vue-component vuejs2

7
推荐指数
1
解决办法
7685
查看次数

Express - 路线分离方法

我正在尝试找到在Express中分隔路由的最佳方法.我知道有两种方法,我想知道它们之间有什么区别,哪一种被认为是"最佳实践"(以及为什么).

方法一 - 出口

很多例子似乎都使用这种方法

app.js

var user = require('./routes/users');
app.get('/users', user.list);
Run Code Online (Sandbox Code Playgroud)

路线/ users.js

exports.list = function(req, res){
  res.render('users', { title: 'Users', users: users });
};
Run Code Online (Sandbox Code Playgroud)

方法二 -

此方法用于快速指南

app.js

var users = require('./routes/users');
app.use('/users', users);
Run Code Online (Sandbox Code Playgroud)

路线/ users.js

var express = require('express');
var router = express.Router();

router.get('/', function(req, res){
  res.render('users', { title: 'Users', users: users });
});

module.exports = router;
Run Code Online (Sandbox Code Playgroud)

正如我上面所说,哪一个是首选方法,为什么?

javascript routes node.js express

5
推荐指数
1
解决办法
1978
查看次数

连接到MySQL数据库时出现身份验证插件错误

我正在研究macOS ver 10.13.5--实际上是最新的.

我经常使用Laravel项目(5.6),因此我使用brew服务isntaller来使本地环境工作.

我在v.2.0.12中使用PHP v 7.1.16,MySQL 8.0.11和Valet

通过Brew安装PHP和MySQL .

我过去3个月一直没有任何问题,但是当我不得不重新安装我的操作系统的那一天终于来了.

在使用新副本重新安装macOS并以与以前相同的版本安装每个服务之后,我在尝试通过SequelPro连接到MySQL数据库时遇到了一些奇怪的错误.

这是一些日志:

MySQL said: Authentication plugin 'caching_sha2_password' cannot be 
loaded: dlopen(/usr/local/lib/plugin/caching_sha2_password.so, 2): image 
not found
Run Code Online (Sandbox Code Playgroud)

这很奇怪,因为我一直在使用相同的东西,没有任何问题......

所以我很感激有关如何处理这个问题的任何建议.另外我还想注意,我几乎已经阅读了关于这个问题的所有内容,但是我无法找到至少一个像我这样的配置(大多数人都在使用mysql提供的本地数据库时描述了这个问题)其他一些服务提供商).

mysql homebrew laravel sequelpro

5
推荐指数
1
解决办法
3326
查看次数

Mongoose方法未定义

我正在尝试为我的用户模型创建一个'checkPassword'方法,但每当我调用它时,我都会收到以下错误:

User.checkPassword(password, hash, function(err, samePassword){
             ^
TypeError: undefined is not a function
Run Code Online (Sandbox Code Playgroud)

我对猫鼬很新,所以我不确定我哪里出错了.

users.js(用户模型)

var mongoose = require('mongoose'),
Schema = mongoose.Schema,
bcrypt = require('bcrypt');


var userSchema = new Schema({
 email : {type: String, required: true, unique: true},
 password : {type: String, required: true},
 firstName : {type: String},
 lastName : {type: String}
});


userSchema.methods.checkPassword = function checkPassword(password, hash, done){

    bcrypt.compare(password, hash, function(err, samePassword) {
      if(samePassword === true){
        done(null, true);
      } else {
        done(null, false)
      }
    });
}

module.exports = mongoose.model('User', userSchema); …
Run Code Online (Sandbox Code Playgroud)

mongoose mongodb node.js

4
推荐指数
1
解决办法
3363
查看次数

在Express中提供静态文件 - 它是否被缓存?

在ExpressJS 文档中,没有关于如何加载文件的信息.

我的问题是:当我请求一个静态文件时,是否每次请求都从磁盘加载文件?或者以某种方式缓存?如果是这样,怎么样?

node.js express

4
推荐指数
1
解决办法
1620
查看次数

Python 虽然不是 True 或 False

如果提供的值不是“是”或“否”,是否可以使用 while 循环来不断询问输入值?例如

someVar = None

while someVar is not (True or False):

  someVar = str.lower(input())

  if someVar == 'yes':
      someVar = True

  elif someVar== 'no':
      someVar = False
Run Code Online (Sandbox Code Playgroud)

python python-3.x

4
推荐指数
1
解决办法
2万
查看次数

没有Express的简单Node.js服务器 - 管理HTML文件以响应

我正在努力制作一个没有Express的简单Node.js服务器,这实际上我实际上学习了更多关于基于路径请求和基本HTTP内容的实际服务器文件和请求数据的故障.

我有这样简单的服务器,如下所示:

 var http = require('http');

const PORT = 6969;

var allRoutes = require('./routes/all');

var server = http.createServer(allRoutes);

server.listen(PORT, function () {
    console.log("Server listening on: http://localhost:%s", PORT);
});
Run Code Online (Sandbox Code Playgroud)

然后我有一个像这样处理所有请求的"中间件"函数:

var url = require('url');
var fs = require('fs');
var appRootPath = require('app-root-path');
var path = require('path');

function handleRequest(req, res) {

    var requestUrl = url.parse(req.url);


    var fsPath;
    if (requestUrl.pathname === '/') {
        fsPath = path.resolve(appRootPath + '/view/index.html');
    }
    else {
        fsPath = path.resolve(appRootPath + '/view/' + requestUrl.pathname);
    }

    fs.stat(fsPath, function (err, stat) …
Run Code Online (Sandbox Code Playgroud)

javascript node.js

1
推荐指数
1
解决办法
910
查看次数

res.sendFile 强制下载文件

我正在尝试发送要在浏览器中全屏显示的图像,但问题是以下代码强制将文件作为附件(已下载)发送,类似于res.download()而不是显示图像:

res.sendFile(file, {root: path.join(__dirname, '../storage')})
Run Code Online (Sandbox Code Playgroud)

有没有办法在不强制下载或不读取原始文件的情况下显示图像?

node.js express

1
推荐指数
1
解决办法
4366
查看次数