Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 下载时返回损坏的文档(瓶子和pymongo)?_Python_Mongodb_Python 3.x_Bottle_Gridfs - Fatal编程技术网

Python 下载时返回损坏的文档(瓶子和pymongo)?

Python 下载时返回损坏的文档(瓶子和pymongo)?,python,mongodb,python-3.x,bottle,gridfs,Python,Mongodb,Python 3.x,Bottle,Gridfs,我在“/add”有一个基本的web表单,它接受用户输入并上传到mongodb: <body> <br><br><br><br> %for fileobj in files: Filename: {{fileobj['filename']}}<br> Size: {{fileobj['length']}}<br> Date Uploaded: {{fileobj['uploadDate']

我在“/add”有一个基本的web表单,它接受用户输入并上传到mongodb:

<body>

<br><br><br><br>
%for fileobj in files:
    Filename: {{fileobj['filename']}}<br>
    Size: {{fileobj['length']}}<br>
    Date Uploaded: {{fileobj['uploadDate']}}<br>
    md5: {{fileobj['md5']}}<br>
    <a href="download?id={{fileobj['_id']}}">&lt--Download File--&gt</a>
<br><br><br><br>
%end
</body>
PYTHON:

@bottle.route('/add')
def add_page():
    return bottle.template('files_test/add')

@bottle.route('/upload', method='POST')
def do_upload():
    data = request.files.data
    if data:
        raw = data.file.read()  # This is dangerous for big files
        file_name = data.filename
        try:
            newfile_id = fs.put(raw, filename=file_name)
        except:
            return "error inserting new file"
    print(newfile_id)
    return bottle.redirect('/')
<!DOCTYPE html>

<!-- pass in files (dict)-->
<html>
<head>
    <title>Preformance and Capacity Management</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="data" /><br>
    <input type="submit"/>
</form>
</body>
</html>
@bottle.route('/download')
def download():
    file_id = ObjectId(bottle.request.query.id)
    if file_id:
        try:
            file_to_download = fs.get(file_id)
        except:
            return "document id not found for id:" + file_id, sys.exc_info()[0]
        file_extension = str(file_to_download.name)[(str(file_to_download.name).index('.')):]


        response.headers['Content-Type'] = (mimetypes.types_map[file_extension])
        response.headers['Content-Disposition'] = 'attachment; filename=' + file_to_download.name
<body>

<br><br><br><br>
%for fileobj in files:
    Filename: {{fileobj['filename']}}<br>
    Size: {{fileobj['length']}}<br>
    Date Uploaded: {{fileobj['uploadDate']}}<br>
    md5: {{fileobj['md5']}}<br>
    <a href="download?id={{fileobj['_id']}}">&lt--Download File--&gt</a>
<br><br><br><br>
%end
</body>
添加第三方物流:

@bottle.route('/add')
def add_page():
    return bottle.template('files_test/add')

@bottle.route('/upload', method='POST')
def do_upload():
    data = request.files.data
    if data:
        raw = data.file.read()  # This is dangerous for big files
        file_name = data.filename
        try:
            newfile_id = fs.put(raw, filename=file_name)
        except:
            return "error inserting new file"
    print(newfile_id)
    return bottle.redirect('/')
<!DOCTYPE html>

<!-- pass in files (dict)-->
<html>
<head>
    <title>Preformance and Capacity Management</title>
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="data" /><br>
    <input type="submit"/>
</form>
</body>
</html>
@bottle.route('/download')
def download():
    file_id = ObjectId(bottle.request.query.id)
    if file_id:
        try:
            file_to_download = fs.get(file_id)
        except:
            return "document id not found for id:" + file_id, sys.exc_info()[0]
        file_extension = str(file_to_download.name)[(str(file_to_download.name).index('.')):]


        response.headers['Content-Type'] = (mimetypes.types_map[file_extension])
        response.headers['Content-Disposition'] = 'attachment; filename=' + file_to_download.name
<body>

<br><br><br><br>
%for fileobj in files:
    Filename: {{fileobj['filename']}}<br>
    Size: {{fileobj['length']}}<br>
    Date Uploaded: {{fileobj['uploadDate']}}<br>
    md5: {{fileobj['md5']}}<br>
    <a href="download?id={{fileobj['_id']}}">&lt--Download File--&gt</a>
<br><br><br><br>
%end
</body>
home.tpl:

<body>

<br><br><br><br>
%for fileobj in files:
    Filename: {{fileobj['filename']}}<br>
    Size: {{fileobj['length']}}<br>
    Date Uploaded: {{fileobj['uploadDate']}}<br>
    md5: {{fileobj['md5']}}<br>
    <a href="download?id={{fileobj['_id']}}">&lt--Download File--&gt</a>
<br><br><br><br>
%end
</body>





%对于文件中的fileobj: 文件名:{{fileobj['Filename']}}
大小:{{fileobj['length']}}
上载日期:{{fileobj['uploadDate']}}
md5:{{fileobj['md5']}}




%结束
当我试图下载这些文件时,它会下载它们,但我无法打开它们。例如,一个内部有“test”字样的文本文件将打开为空白。excel电子表格将打开

<body>

<br><br><br><br>
%for fileobj in files:
    Filename: {{fileobj['filename']}}<br>
    Size: {{fileobj['length']}}<br>
    Date Uploaded: {{fileobj['uploadDate']}}<br>
    md5: {{fileobj['md5']}}<br>
    <a href="download?id={{fileobj['_id']}}">&lt--Download File--&gt</a>
<br><br><br><br>
%end
</body>

我上传或下载错误吗?或者两者都有?有关完整代码,请参见“代码审阅”部分。

在“下载”功能的“server.py”文件中添加以下行:

<body>

<br><br><br><br>
%for fileobj in files:
    Filename: {{fileobj['filename']}}<br>
    Size: {{fileobj['length']}}<br>
    Date Uploaded: {{fileobj['uploadDate']}}<br>
    md5: {{fileobj['md5']}}<br>
    <a href="download?id={{fileobj['_id']}}">&lt--Download File--&gt</a>
<br><br><br><br>
%end
</body>
return file_to_download

我需要设置HTTP头,然后返回原始数据。

解决了这个问题。返回数据本身(在我的例子中是“文件到下载”)。
<body>

<br><br><br><br>
%for fileobj in files:
    Filename: {{fileobj['filename']}}<br>
    Size: {{fileobj['length']}}<br>
    Date Uploaded: {{fileobj['uploadDate']}}<br>
    md5: {{fileobj['md5']}}<br>
    <a href="download?id={{fileobj['_id']}}">&lt--Download File--&gt</a>
<br><br><br><br>
%end
</body>