没有这样的文件或目录:/ uploads

时间:2016-03-15 15:37:08

标签: python flask upload

我看了几个其他问题,但无法弄清楚出了什么问题。我收到以下错误:“FileNotFoundError:[Errno 2]没有这样的文件或目录:'/ uploads\MRRtest.csv'”任何人都可以帮忙吗?错误消息的正斜率和反斜杠之间有什么区别?

由于

from flask import Flask, render_template, request, redirect, url_for, flash
from flask.ext.bootstrap import Bootstrap
from werkzeug import secure_filename
import os


app = Flask(__name__)
bootstrap = Bootstrap(app)
UPLOAD_FOLDER = '/uploads'
ALLOWED_EXTENSIONS = set(['csv'])
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER



def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
            return redirect(url_for('uploaded_file', filename=filename))
    return render_template('index.html')

我的index.html模板如下:

{% extends "base.html" %}

{% block title %}Flasky{% endblock %}

{% block page_content %}
<div class="page-header">
    <h1>Upload File</h1>
    <form action="" method=post enctype=multipart/form-data>
      <p><input type=file name=file>
         <input type=submit value=Upload>
    </form>
</div>
{% endblock %}

2 个答案:

答案 0 :(得分:1)

var books = books.Where(x => x.author == search).ToList(); 表示绝对链接(C:/ upload),因此您应该使用/uploads代替。

另外,您可以使用https://stackoverflow.com/a/20257725/5851179

中的精彩片段
upload/

答案 1 :(得分:0)

从评论中我意识到我的uploads目录与run.py文件位于同一目录中,而不是index.html运行的'templates'目录。我修改了

APP_ROOT = os.path.dirname(os.path.abspath(__file__))
UPLOAD_FOLDER = os.path.join(APP_ROOT, 'static/uploads')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

UPLOAD_FOLDER = '/uploads'

我现在正在构建“url for endpoint”

感谢您的帮助。