久久久久久久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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产真实精品久久二三区 | 欧美一区二区在线观看 | 伊人精品在线 | 一级毛片黄片 | 国产精品欧美一区二区三区 | 久久久久久免费毛片精品 | 久久久久久国产精品 | 一区二区三区久久 | 国产福利在线 | 国产午夜精品久久久久 | 亚州精品天堂中文字幕 | 欧美一区二区三区在线视频 | 黄免费看| 午夜激情在线视频 | 一本大道久久a久久精二百 欧洲一区二区三区 | 久久久爽爽爽美女图片 | 一级黄色影片在线观看 | 亚洲天堂男人的天堂 | 国产精品一区二区三区久久久 | 日本三级全黄三级a | 少妇一区在线观看 | 国产成人精品久久二区二区91 | 久久国产精品视频 | 国产一级视频免费播放 | 精品成人av | 国产美女黄色 | 九色www| 午夜在线免费观看 | 欧美日韩国产高清视频 | 一区二区三区免费在线观看 | 欧美日韩国产一区二区三区 | 日本超碰 | 2020国产在线 | 久久一区二区精品 | 浴室洗澡偷拍一区二区 | 久久国产精品99久久久久 | 久久久久亚洲精品 | 在线视频国产一区 | 成人在线免费观看 | 日韩插插 | 国产成人精品久久二区二区 |