檔案上傳 ContentType請求格式

2022-06-11 18:54:09 字數 1889 閱讀 3465

1、通過表單形式上傳檔案

1

<

form

action

=' /file_put/

' method

='post'

enctype

=' multipart/form-data'

>

2 檔名<

input

type

='file'

name

='file_obj'

>

3<

input

type

='submit'

>

4form

>

表單上傳檔案需要設定屬性:enctype

url.py 檔案路徑如下:

path('file_put',views.file_put)

檢視函式

1

deffile_put(request):2#

獲取到上傳的檔案物件

3 file_obj=request.files.get('

file_obj')

4#檔案有乙個name屬性,獲取檔名稱字串

5print

(file_obj.name)

6 path=file_obj.name

7   #拼接路徑

2、通過ajax上傳檔案

1 檔案<

input

type

="file"

name

="file_obj"

id="file"

>

2<

input

type

="button"

class

="filbtn"

value

="提交"

>

3<

p class

="msg"

>

p>45

<

script

>6$(

function

() 23}24

2526

})27

})28

})29

script

>

檢視函式同上

contenttype在請求頭里,告訴瀏覽器是什麼請求格式,

請求格式的分類

urlencoded:預設的請求格式 use='bart'&pwd='123'

formdata:上傳檔案

json格式:

1

$.ajax()

1112 processdata: false , //不處理資料

13 contenttype: false, //不設定內容型別

1415

16 })

注:django 只解析urlencoded格式的資料

傳送json資料只能手動解

解析json資料:

data=request.body.decode('utf8)解碼

json_data=json.loads(data) 反序列化

各種檔案對應的ContentType,拿來即用

contenttype屬性指定響應的 http內容型別。如果未指定 contenttype,預設為text html。我們在 也經常需要定義contenttype,用於指定響應的型別 例 response.setcharacterencoding utf 8 response.setcontentt...

content type 型別講解

content type mediatype,即是internet media type,網際網路 型別 也叫做mime型別,在http協議訊息頭中,使用content type來表示具體請求中的 型別資訊。html view plain 型別格式 type subtype parameter ty...

Content Type,內容型別

content type,內容型別,一般是指網頁中存在的content type,contenttype屬性指定請求和響應的http內容型別。如果未指定contenttype,預設為text html。文字方式的網頁檔案。窗體資料以純文字形式進行編碼,其中不含任何控制項或格式字元。空格轉換為 加號,...