Django(part47)--文件上傳

學習筆記,僅供參考



文件上傳


上傳須知


  • 文件上傳必須爲POST提交方式,在文件上傳時,表單<form>中必須有帶有enctype="multipart/form-data" 參數,纔會包含文件內容數據。

  • 在表單中,我們通常用<input type="file" name="xxx">標籤上傳文件,type="file"用於生產一個表單控件,而name="xxx"與服務器端相聯繫。

    • 在服務器端,我們可以通過request.FILES['xxx'] 返回的文件流對象,拿到name="xxx"所對應的上傳文件。

    • 若我們用變量名file接收request.FILES['xxx'],即file=request.FILES['xxx'] ,此時,file就會綁定一個文件流對象,我們可以通過file.name 獲取上傳文件的文件名,通過file.file 獲取上傳文件的字節流對象(相當於f=open("文件名", rb)f獲取的對象)。

  • 如上傳文件爲圖片類型,則可以定義成models.ImageField類型

    • image_file = models.ImageField(upload_to='images/')
    • 如果屬性類型爲ImageField需要安裝Pilow,pip install Pillow

  • 上傳文件的表單書寫方式

<!-- file:static/upload.html -->
<html>
<head>
    <title>文件上傳</title>
    <meta charset="utf-8">
</head>
<body>
    <h1>上傳文件</h1>
    <form method="post" action="/upload/" enctype="multipart/form-data">
        <input type="file" name="myfile"/><br>
        <input type="submit" value="上傳">
    </form>
</body>
</html>

舉個例子


現在,在這個例子中,我想實現文件的上傳。


現在,我們在mywebsite_bookstore項目下的模板文件夾templates下創建一個upload.html文件:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>上傳文件</title>
</head>
<body>
	<h2>文件上傳</h2>
	<form action="/upload/" method="POST" enctype="multipart/form-data">
		<input type="file" name="myfile">
		<input type="submit" value="上傳">

	</form>
	
</body>
</html>

在主視圖模塊views.py中添加一個視圖函數:

def on_upload(request):
    if request.method == "GET":
        return render(request, "upload.html")
    elif request.method == "POST":
        file = request.FILES['myfile']
        html = "上傳的文件爲:{} <br> 文件名爲{}".format(file, file.name)
        return HttpResponse(html)

注意,我們獲取的file對象不在我的磁盤上,而在內存中。


在主urls.py模塊中添加一個路由:

from django.urls import re_path
from django.contrib import admin
from django.conf.urls import include
from . import views

urlpatterns = [
    re_path(r'^admin/', admin.site.urls),
    re_path(r'^upload/$',views.on_upload),
]

現在,我們向http://127.0.0.1:8000/upload/發起請求,並選擇要上傳的文件:


點擊上傳,得到如下頁面:


現在,我想將上傳的圖片TX.jpg保存在mywebsite_bookstore項目下的static/image文件夾下。


我們修改一下views.py文件:

def on_upload(request):
    if request.method == "GET":
        return render(request, "upload.html")
    elif request.method == "POST":
        file = request.FILES['myfile']
        html = "上傳的文件爲:{} <br> 文件名爲{}".format(file, file.name)
        with open(r"F:\MyStudio\PythonStudio\goatbishop.project01\Django\mywebsite_bookstore\static\image\\"
                  + file.name, 'wb') as f:
            b = file.file.read()
            #讀取緩存文件的內容
            f.write(b)
        return HttpResponse(html)

注意,這裏的文件保存路徑爲絕對路徑。我們也可以通過BASE_DIR來寫保存文件的絕對路徑(BASE_DIR會告訴我們項目所在的絕對路徑)。

要做這件事,我們就需要在settings.py中自定義一個文件保存路徑:

#自定義變量UPLOAD_DIR用於綁定上傳文件的保存路徑
UPLOAD_DIR = os.path.join(BASE_DIR, 'static/image')

在主views.py中導入當前項目的配置文件settings.py,並修改視圖函數on_upload:

from django.conf import settings
import os
def on_upload(request):
    if request.method == "GET":
        return render(request, "upload.html")
    elif request.method == "POST":
        file = request.FILES['myfile']
        html = "上傳的文件爲:{} <br> 文件名爲{}".format(file, file.name)
        with open(os.path.join(settings.UPLOAD_DIR, file.name), 'wb') as f:
            b = file.file.read()
            f.write(b)
        return HttpResponse(html)

一般情況下,我們的數據庫中不會保存文件本身,而只保存文件的一些信息(比如文件名和文件保存路徑)。


我們向http://127.0.0.1:8000/upload/發起請求,並上傳文件,查看一下我們的F:\MyStudio\PythonStudio\goatbishop.project01\Django\mywebsite_bookstore\static\image\路徑下是否有TX.jpg文件:

Very Well !

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章