解決什么問題舒帮?
減少請(qǐng)求大小 服務(wù)端在響應(yīng)的時(shí)候一般都有 gzip 瀏覽器會(huì)幫我們解壓 也就是說下載很快
但是上傳數(shù)據(jù)卻沒有 gzip
參考 https://imququ.com/post/how-to-compress-http-request-body.html
python 解碼 zlib 庫
https://docs.python.org/2/library/zlib.html
瀏覽器端 壓縮 pako庫
https://github.com/nodeca/pako
可能遇到的問題
# to (de-)compress deflate format, use wbits = -zlib.MAX_WBITS
# to (de-)compress zlib format, use wbits = zlib.MAX_WBITS
# to (de-)compress gzip format, use wbits = zlib.MAX_WBITS | 16
瀏覽器端
var behaviors = pako.gzip('scscsccscs\r\n');
behaviors = new Blob([behaviors])
var formData = new FormData();
formData.append('behaviors', behaviors);
formData.append('id', 123456);
var formDataObj = $.ajax({
url: 'http://localhost:9727/submit/',
type: 'POST',
data: formData,
processData: false,
contentType: false
})
django server 端
import zlib
behvs = request.FILES.getlist('behaviors')[0]
behvs = request.FILES['behaviors']
behvs = behvs.read() #必須 將inmemoryuploadedfile 轉(zhuǎn)為 buffer
print('behaviors', behvs)
decom = zlib.decompress(behvs, 16 + zlib.MAX_WBITS )
print('behaviors', decom)