【问题标题】:How to upload pdf file on HTML and process it at the FLASK backend?如何在 HTML 上上传 pdf 文件并在 FLASK 后端进行处理?
【发布时间】:2020-07-03 03:04:19
【问题描述】:

我想知道将 PDF 文件作为 HTML 表单的输入并将其发送到我的 Flask 应用程序以处理此文件的最简单方法是什么。有什么方法可以避免在我的系统上本地保存文件?

HTML:

<!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form method=post enctype=multipart/form-data action = 'http://127.0.0.1:5000/uploadDoc'>
      <input type=file name=file>
      <input type=submit value=Upload>
    </form>

烧瓶:

app = Flask(__name__)

UPLOAD_FOLDER = 'C:\\Users\\Tanoy Majumdar\\Documents\\Chekk_OCR\\'


@app.route('/')
def hello_world():
    return 'Hello, World!'

@app.route('/uploadDoc', methods = ['POST', 'GET'])
def upload_file():
    print("Hello")
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file:
            filename = secure_filename(file.filename)
            #file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
    print(str(filename))  
    print (generate_kv(filename)) #generate_kv() converts pdf pages to images using pdf2image lib.

运行此应用时出现以下错误:

[2020-07-03 10:52:16,364] 应用程序中的错误:/uploadDoc [POST] 上的异常 回溯(最近一次通话最后): 文件“c:\users\tanoy majumdar\anaconda3\lib\site-packages\pdf2image\pdf2image.py”,第 436 行,在 pdfinfo_from_path 引发 ValueError 值错误

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次):

  File "c:\users\tanoy majumdar\anaconda3\lib\site-packages\flask\app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\users\tanoy majumdar\anaconda3\lib\site-packages\flask\app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\users\tanoy majumdar\anaconda3\lib\site-packages\flask\app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\users\tanoy majumdar\anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "c:\users\tanoy majumdar\anaconda3\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\users\tanoy majumdar\anaconda3\lib\site-packages\flask\app.py", line 1936, in dispatch_request    
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\Tanoy Majumdar\Documents\Chekk_OCR\runner.py", line 36, in upload_file
    print (generate_kv(filename))
  File "C:\Users\Tanoy Majumdar\Documents\Chekk_OCR\new_try.py", line 102, in generate_kv
    pages = convert_from_path(pdf_file ,dpi )
  File "c:\users\tanoy majumdar\anaconda3\lib\site-packages\pdf2image\pdf2image.py", line 94, in convert_from_path
    page_count = pdfinfo_from_path(pdf_path, userpw, poppler_path=poppler_path)["Pages"]
  File "c:\users\tanoy majumdar\anaconda3\lib\site-packages\pdf2image\pdf2image.py", line 446, in pdfinfo_from_path
    "Unable to get page count.\n%s" % err.decode("utf8", "ignore")
pdf2image.exceptions.PDFPageCountError: Unable to get page count.
Syntax Error: Document stream is empty

【问题讨论】:

    标签: javascript python html flask


    【解决方案1】:

    如果不知道 generate_kv 需要什么参数,就无法 100% 回答这个问题。文件名?一份文件?路径?字节流?

    所以我假设你必须传入file.stream,它是一个类似对象的文件,或者file.stream.read(),它是一个流。

    如果generate_kv 需要路径,您可能必须先将file 保存到磁盘。

    顺便说一句,您的 file 实际上是 werkzeug.datastructure.filestorage 类型 - 更多信息

    https://werkzeug.palletsprojects.com/en/1.0.x/datastructures/#werkzeug.datastructures.FileStorage

    更新

    我刚刚注意到,还有file._file,它实际上是一个真正的类文件对象,而file.streamSpooledTemporaryFile

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 2017-11-26
      • 2010-09-26
      相关资源
      最近更新 更多