久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

淺析Django 接收所有文件,前端展示文件(包括視頻,文件,圖片)ajax請求

瀏覽:62日期:2024-10-23 08:00:36

如果是后臺上傳文件:

setting配置:

STATIC_URL = ’/static/’STATICFILES_DIRS = [ os.path.join(BASE_DIR, ’static’), os.path.join(BASE_DIR, 'media'),]# Django用戶上傳的都叫media文件MEDIA_URL = '/media/'# media配置,用戶上傳的文件都默認放在這個文件夾下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' /> <input type='button' value='提交' onclick='upload()'/></form><br><br><br><br><div>顯示圖片 <img id='images'></div><br><br><br><br><div>顯示路徑 <a href='http://www.baoyu77737.com/bcjs/17526.html' id=' rel='external nofollow' imagess'>鏈接</a></div></div><br><br><br><br><div> {# href='http://www.baoyu77737.com/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,//用于對data參數進行序列化處理 這里必須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 APIViewfrom django.shortcuts import render, redirect, HttpResponsefrom dal import modelsfrom django.http import JsonResponseclass 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 urlquotefrom rest_framework.views import APIViewfrom django.shortcuts import render, redirect, HttpResponsefrom dal import modelsfrom django.http import JsonResponse, FileResponse, StreamingHttpResponseclass 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

總結

到此這篇關于Django 接收所有文件 前端展示文件(包括視頻,文件,圖片)ajax請求的文章就介紹到這了,更多相關django 接收所有文件內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Django
主站蜘蛛池模板: 清水河县| 贵港市| 诸暨市| 平阳县| 介休市| 白河县| 鄂伦春自治旗| 文水县| 朝阳区| 赣榆县| 望谟县| 九龙城区| 泾阳县| 梓潼县| 深水埗区| 沈丘县| 泉州市| 高台县| 青神县| 杭锦旗| 浙江省| 香港| 新民市| 藁城市| 莱西市| 娱乐| 镇安县| 绍兴县| 普兰县| 桐柏县| 石景山区| 沧州市| 昌平区| 张掖市| 卢龙县| 高雄市| 津市市| 金坛市| 双柏县| 昭平县| 锡林郭勒盟|