【问题标题】:How to upload image to folder and name to database using python如何使用python将图像上传到文件夹并将名称上传到数据库
【发布时间】:2020-12-10 13:30:29
【问题描述】:

这是我将图像上传到文件夹并将名称上传到数据库的代码,但它不起作用。 我收到一个错误 'bytes' 对象没有属性 'image' 发生异常

 #html code  -->#

<html>
    <head>
        <TITLE> Product Image</TITLE>
    </head>
    <body>
        <form enctype="multipart/form-data" action="productimg.py" method=post>
            <table align=center  cellspacing=20 cellpadding=10 >
                <th align="center" colspan=3 > <u>Upload Image of the Goods</u></th>
                <tr ><th  colspan="4"> Enter your details below and wait minimum for half an hour.</th></tr>
                <tr>
                    <td> Product Image  :  </td><td> <input type=file name="image" accept = "image/*"    accept=".png/*" value="image"> </td>

                </tr>               
                <tr>
                    <td> </td> <td> <input type=Submit > <input type=Reset> </td>
                </tr>
            </table>
        </form>
    </body>
 </html>

我收到一个错误 'bytes' 对象没有属性 'image' 异常发生 在 pyhton 代码中。

#python代码-->#

#!C:\Users\Pc\AppData\Local\Programs\Python\Python38-32\python.exe
print('Content-type:text/html\n\r')
import cgi
import sys
from myconnect import *
message=0
try:
    con,cur=myconnect()
    form= cgi.FieldStorage()
    # Get  here.
    fileitem=form.getvalue('image');
    #fileitem = form['image']
    # Test if the file was uploaded
    if fileitem.image:
        # strip leading path from file name to avoid
        # directory traversal attacks
        fn = os.path.basename(fileitem.image)
        open('/uploads/' + fn, 'wb').write(fileitem.file.read())
        message = 'The file "' + fn + '" was uploaded successfully'
        query=f"select product_delivery_id from tbl_product_deliver order by product_delivery_id desc"  
        cur.execute(query) 
        pid=cur.fetchone()

        update=f"UPDATE `tbl_product_deliver` SET `product_image`='{fn}' WHERE `product_delivery_id`='{pid}'"
        cur.execute(update)
        con.commit()
    else:
       message = 'No file was uploaded'
except Exception as e:
    print(e)
    print("Exception occured")

【问题讨论】:

    标签: python database image upload using


    【解决方案1】:
    1. 你不能使用像 php 这样的 python 脚本。通常,您必须配置一个等待后请求的事件处理程序。为了在 python 中执行此操作,您需要一个像 flaskbottle 这样的框架来接收发布请求,而不是 productimg.py 你使用你的路线'已将此框架设置为 html 中的 action

    2. 您的 FieldStorage 对象 是在没有参数的情况下调用的,因此您创建了一个没有数据的空 FieldStorge 对象。

    接收发布请求的 FieldStorage 对象如下所示:

    fs= FieldStorage(fp=request.body, headers=request.headers, environ={'REQUEST_METHOD':'POST', 'CONTENT_TYPE':request.headers['Content-Type'], })
    

    但请确保您在执行问题 2 之前解决了问题 1。

    用瓶子使用这个python代码:

    from bottle import post, request, run
    @post('/upload')
    def getImage():
        #printing the raw data
        print(request.body)
        #insert your code here
    run(host='localhost', port=8080)
    

    并在 html 中进行更改: action="http://localhost:8080/upload

    祝你好运

    【讨论】:

    • 我们是否可以简单地使用文件处理将图像插入文件夹并将名称插入数据库,或者是否有任何其他方法不使用烧瓶和瓶子,因为我是python的初学者
    • 如果你想使用 html,我不知道比使用其中一种更简单的方法。您刚刚安装了 pip 并调用 pip install bottle 像我添加到我的答案一样。
    • 如果您不需要使用任何用户界面,您可以使用file = open("example.png", "r")打开文件
    • 或使用任何其他 UI,如 Tk(我对 python UI 没有经验)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 2020-11-12
    • 2016-09-07
    • 1970-01-01
    • 2014-02-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多