久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用 Python 上傳文件

Upload a File with Python(使用 Python 上傳文件)
本文介紹了使用 Python 上傳文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個 HTML 表單,我正在使用 Python 根據輸入生成一個日志文件.如果用戶愿意,我還希望能夠允許用戶上傳圖片.一旦它在那里,我可以弄清楚如何用 Python 操作它,但我不確定如何上傳圖像.這肯定是以前做過的,但我很難找到任何例子.你們誰能指出我正確的方向嗎?

I have an HTML form and I am using Python to generate a log file based on the input. I'd like to also be able to allow the user to upload an image if they choose. I can figure out how to manipulate it with Python once it's there, but I'm not sure how to get the image uploaded. This has most certainly been done before, but I'm having a hard time finding any examples. Can any of you point me in the right direction?

基本上,我使用 cgi.FieldStoragecsv.writer 來制作日志.我想從用戶的計算機上獲取圖像,然后將其保存到我服務器上的目錄中.然后我將重命名它并將標題附加到 CSV 文件中.

Basically, I'm using cgi.FieldStorage and csv.writer to make the log. I want to get an image from the user's computer and then save it to a directory on my server. I will then rename it and append the title to the CSV file.

我知道這有很多選擇.我只是不知道它們是什么.如果有人可以指導我獲取一些資源,我將非常感激.

I know there are a lot of options for this. I just don't know what they are. If anyone could direct me toward some resources I would be very appreciative.

推薦答案

既然你說你的特定應用程序是與 python cgi 模塊一起使用的,那么快速谷歌就會找到很多例子.這是第一個:

Since you said that your specific application is for use with the python cgi module, a quick google turns up plenty of examples. Here is the first one:

最小 http 上傳 cgi(Python 配方) (剪輯)

def save_uploaded_file (form_field, upload_dir):
    """This saves a file uploaded by an HTML form.
       The form_field is the name of the file input field from the form.
       For example, the following form_field would be "file_1":
           <input name="file_1" type="file">
       The upload_dir is the directory where the file will be written.
       If no file was uploaded or if the field does not exist then
       this does nothing.
    """
    form = cgi.FieldStorage()
    if not form.has_key(form_field): return
    fileitem = form[form_field]
    if not fileitem.file: return
    fout = file (os.path.join(upload_dir, fileitem.filename), 'wb')
    while 1:
        chunk = fileitem.file.read(100000)
        if not chunk: break
        fout.write (chunk)
    fout.close()

此代碼將獲取文件輸入字段,該字段將是一個類似文件的對象.然后它將逐塊讀取到輸出文件中.

This code will grab the file input field, which will be a file-like object. Then it will read it, chunk by chunk, into an output file.

2015 年 4 月 12 日更新:根據評論,我添加了對這個舊的 activestate 片段的更新:

Update 04/12/15: Per comments, I have added in the updates to this old activestate snippet:

import shutil

def save_uploaded_file (form_field, upload_dir):
    form = cgi.FieldStorage()
    if not form.has_key(form_field): return
    fileitem = form[form_field]
    if not fileitem.file: return

    outpath = os.path.join(upload_dir, fileitem.filename)

    with open(outpath, 'wb') as fout:
        shutil.copyfileobj(fileitem.file, fout, 100000)

這篇關于使用 Python 上傳文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产成人免费视频 | 亚洲欧美在线视频 | 青青草原综合久久大伊人精品 | av国产精品| 午夜日韩视频 | 国产在线观看福利 | 久久精品小视频 | 亚洲天堂一区 | 伊人影院在线观看 | 成人黄视频在线观看 | xxxcom在线观看| 亚洲男女视频在线观看 | 美女高潮网站 | 亚洲精品久久国产高清情趣图文 | 成人深夜小视频 | 99热精品在线观看 | 久久久久久久网 | 久久亚洲国产 | 无码一区二区三区视频 | 亚洲一区二区免费视频 | 亚洲一区二区三区四区在线观看 | 一级特黄网站 | 久久精品国产久精国产 | 精品国产乱码久久久久久影片 | 亚洲视频一区在线观看 | 国产精品亚洲视频 | 日韩久草 | 天天干狠狠操 | 人人人人干| 碰碰视频| 欧美精品久久 | 精品国产高清一区二区三区 | 一级毛片在线播放 | 欧美成人精品一区二区男人看 | 香蕉大人久久国产成人av | 日韩毛片在线观看 | 国产第一区二区 | 国产亚洲一区精品 | 日韩中文字幕一区 | 亚州精品成人 | 国产99久久久国产精品 |