淺析Django 接收所有文件,前端展示文件(包括視頻,文件,圖片)ajax請(qǐng)求
如果是后臺(tái)上傳文件:
setting配置:
STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), os.path.join(BASE_DIR, "media"), ] # Django用戶上傳的都叫media文件 MEDIA_URL = "/media/" # media配置,用戶上傳的文件都默認(rèn)放在這個(gè)文件夾下 MEDIA_ROOT = os.path.join(BASE_DIR, "media") model的配置: img = models.FileField(upload_to="img/",verbose_name="圖片")
接收任何文件的前端代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form method="post" action="/upload/" enctype="multipart/form-data" target="ifm1">
<input type="file" name="file" id="file"/>
<input type="button" value="提交" onclick="upload()"/>
</form>
<br>
<br>
<br>
<br>
<div>顯示圖片
<img id="images">
</div>
<br>
<br>
<br>
<br>
<div>顯示路徑
<a href="" id=" rel="external nofollow" imagess">鏈接</a>
</div>
</div>
<br>
<br>
<br>
<br>
<div>
{# href="/static/img/TC代碼.txt" rel="external nofollow" #}
<a id="up"> 下載文件</a>
</div>
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<script>
function upload() {
var formData = new FormData();
var file = document.getElementById('file').files[0];
formData.append("file", file);
$.ajax({
url: "upload/",
type: "post",
data: formData,
dataType: "json",
cache: false, //上傳文件無需緩存
processData: false,//用于對(duì)data參數(shù)進(jìn)行序列化處理 這里必須false
contentType: false, //必須*/
success: function (data) {
console.log("22", data);
$("#images").attr("src", data.image)
$("#imagess").attr("href", data.image)
}
});
}
$("#up").on("click", function () {
$.ajax({
url: "http://127.0.0.1:8000/down/",
type: "get",
data: {},
success: function (data) {
var $a = $('<a></a>');
$a.attr("href", "http://127.0.0.1:8000/down/");
$("body").append($a);
$a[0].click();
$a.remove();
}
})
});
</script>
</body>
</html>
增加任何文件的后端接口代碼:
from rest_framework.views import APIView
from django.shortcuts import render, redirect, HttpResponse
from dal import models
from django.http import JsonResponse
class ImageShow(APIView):
def post(self, request):
name = str(request.data.get("name"))
message = {}
img_url = "/static/img/{}".format(name)
obj = models.Car.objects.filter(img_url=img_url).first()
if obj :
message['code'] = 200
message['message'] = img_url # 返還路徑
return JsonResponse(message)
下載文件后端:
from django.utils.http import urlquote
from rest_framework.views import APIView
from django.shortcuts import render, redirect, HttpResponse
from dal import models
from django.http import JsonResponse, FileResponse, StreamingHttpResponse
class fileShow(APIView):
def get(self, request):
message = {}
file = open('media/img/TC代碼.txt','rb') # 字符串替換成文件
print("file",file.name)
# file_names = file.name.split('/')[-1]
# print("file_names",file_names)
response = FileResponse(file)
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = "attachment;filename={}".format(urlquote("TC代碼.txt")) # 字符串替換成下載文件
print(response)
return response
總結(jié)
到此這篇關(guān)于Django 接收所有文件 前端展示文件(包括視頻,文件,圖片)ajax請(qǐng)求的文章就介紹到這了,更多相關(guān)django 接收所有文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Django如何與Ajax交互
- Django與AJAX實(shí)現(xiàn)網(wǎng)頁動(dòng)態(tài)數(shù)據(jù)顯示的示例代碼
- 使用AJAX和Django獲取數(shù)據(jù)的方法實(shí)例
- Django查詢優(yōu)化及ajax編碼格式原理解析
- django ajax發(fā)送post請(qǐng)求的兩種方法
- django框架中ajax的使用及避開CSRF 驗(yàn)證的方式詳解
- Django中ajax發(fā)送post請(qǐng)求 報(bào)403錯(cuò)誤CSRF驗(yàn)證失敗解決方案
- Django結(jié)合ajax進(jìn)行頁面實(shí)時(shí)更新的例子
- Django 通過JS實(shí)現(xiàn)ajax過程詳解
- Django利用AJAX技術(shù)實(shí)現(xiàn)博文實(shí)時(shí)搜索
相關(guān)文章
python實(shí)現(xiàn)各進(jìn)制轉(zhuǎn)換的總結(jié)大全
這篇文章主要給大家總結(jié)了python實(shí)現(xiàn)各進(jìn)制轉(zhuǎn)換的相關(guān)資料,其中包括字符串與十六進(jìn)制轉(zhuǎn)換、內(nèi)置函數(shù)hex()與進(jìn)制互轉(zhuǎn)等相關(guān)內(nèi)容,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06
使用Python代碼實(shí)現(xiàn)Linux中的ls遍歷目錄命令的實(shí)例代碼
這次我就要試著用 Python 來實(shí)現(xiàn)一下 Linux 中的 ls 命令, 小小地證明下 Python 的不簡(jiǎn)單,需要的朋友可以參考下2019-09-09
淺談pandas中shift和diff函數(shù)關(guān)系
下面小編就為大家分享一篇淺談pandas中shift和diff函數(shù)關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
python實(shí)現(xiàn)字符串和字典的轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)字符串和字典的轉(zhuǎn)換,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Python計(jì)算一個(gè)文件里字?jǐn)?shù)的方法
這篇文章主要介紹了Python計(jì)算一個(gè)文件里字?jǐn)?shù)的方法,涉及Python文件操作及內(nèi)容遍歷的相關(guān)技巧,需要的朋友可以參考下2015-06-06

