current position:Home>About the problem that the front-end video tag video cannot drag the progress bar fast forward (Django)
About the problem that the front-end video tag video cannot drag the progress bar fast forward (Django)
2022-07-25 09:27:23【Ride Hago to travel】
Because the back end returns the file stream of the video , Not the video file address . When the back-end returns the flow, it should be header Set some parameters , Otherwise, some browsers will have problems ( Such as Google Chrome...).
django Commonly used return file stream writing method :
class VideoAPIView(APIView):
"""
Video view
- Parameter position :
Query parameters
- Request parameters :
- Path parameter :
- `id`: file id Will pass
- Parameters returned successfully :
```
Download the file locally
```
- Failure return parameters :
```
{
"rtCode": 0,
"rtMsg": " Abnormal information ",
"rtData": ""
}
```
"""
def get(self, request, file_info_id):
try:
contents_post = {
"query": {
"match": {
"_id": file_info_id
}
},
"script": {
"source": "ctx._source['visits_nums'] = ctx._source['visits_nums']+1"
}
}
elasticsearch.update_by_query(index=constants.ELASTICSEARCH_INDEX, body=contents_post)
fileinfo_obj = models.FileInfo.objects.filter(file_info_id=file_info_id, delete_flag=FileInfo.DeleteFlagEnum.NOT_DELETE.value, file_type=FileInfo.FileTypeEnum.VIDEO.value)
video_name = fileinfo_obj.first().file_url
except models.FileInfo.DoesNotExist:
raise exceptions.ValidationError(" The data doesn't exist ")
photo_path = constants.VIDEO_PATH
file_url_path = os.path.join(photo_path, video_name)
file_size = os.path.getsize(file_url_path)
response = FileResponse(open(file_url_path, 'rb'))
response['Content-Length'] = str(file_size)
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = "attachment; filename={}".format(urlquote(video_name))
return response
This is for returning the video file stream , Front end use video When displaying labels , There will be a problem that some browsers cannot drag the progress bar fast forward .
solve : In the returned header Add Content-Range and Accept-Ranges Parameters
class VideoAPIView(APIView):
"""
Video view
- Parameter position :
Query parameters
- Request parameters :
- Path parameter :
- `id`: file id Will pass
- Parameters returned successfully :
```
Download the file locally
```
- Failure return parameters :
```
{
"rtCode": 0,
"rtMsg": " Abnormal information ",
"rtData": ""
}
```
"""
def get(self, request, file_info_id):
try:
contents_post = {
"query": {
"match": {
"_id": file_info_id
}
},
"script": {
"source": "ctx._source['visits_nums'] = ctx._source['visits_nums']+1"
}
}
elasticsearch.update_by_query(index=constants.ELASTICSEARCH_INDEX, body=contents_post)
fileinfo_obj = models.FileInfo.objects.filter(file_info_id=file_info_id, delete_flag=FileInfo.DeleteFlagEnum.NOT_DELETE.value, file_type=FileInfo.FileTypeEnum.VIDEO.value)
video_name = fileinfo_obj.first().file_url
except models.FileInfo.DoesNotExist:
raise exceptions.ValidationError(" The data doesn't exist ")
photo_path = constants.VIDEO_PATH
file_url_path = os.path.join(photo_path, video_name)
file_size = os.path.getsize(file_url_path)
response = FileResponse(open(file_url_path, 'rb'))
response['Content-Length'] = str(file_size)
response['Content-Type'] = 'application/octet-stream'
response['Content-Range'] = f'bytes 0-{str(file_size)}/{str(file_size)}'
response['Accept-Ranges'] = 'bytes'
response['Content-Disposition'] = "attachment; filename={}".format(urlquote(video_name))
return response
About Content-Range Parameters
Used in response header , In the hair belt Range After request , The server will be in Content-Range The header returns the currently accepted range and the total size of the file . General format :
Content-Range: bytes (unit first byte pos) - [last byte pos]/[entity legth]
for example :
Content-Range: bytes 0-499/22400
0-499 It refers to the range of data currently sent , and 22400 Is the total size of the file .
And after the response , The returned response header content is also different :
HTTP/1.1 200 Ok( Do not use breakpoint continuation )
HTTP/1.1 206 Partial Content( Use breakpoint continuation )
copyright notice
author[Ride Hago to travel],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/206/202207250919016090.html
The sidebar is recommended
- [Python database development practice - MySQL chapter ⑮] - sorting and de duplication (de duplication) of data table result set
- In Python data mining, what are the methods based on knowledge organization in text similarity calculation?
- In Python data mining, what are the representative methods based on string in text similarity calculation?
- Python web page parsing library: crawl web pages with requests HTML
- How Python converts lists or tuples into strings
- Computer level 2 python (phase 1)
- Niuke network Huawei machine test Python
- Python file problem
- Drawing roses with Python
- Python built-in function print()
guess what you like
Self study Python 45 digital processing function (3)
Python web development (VII): login implementation and function test
How to install python'cfg'module
Python visual data analysis 04. Use of numpy Library
Python visual data analysis 05. Pandas data analysis
Computer level 2 python (phase 1)
Backward traversal Python
Python flaw classification
The download volume of the whole network is 5w+. This' classic 'Python book is highly recommended!
Python tuple
Random recommended
- Python strings and lists
- Matplotlib foundation of Python visualization
- Python keyword
- Python(3)
- Python image processing -- three operation modes to realize image morphological transformation
- [Python cold knowledge you don't know] unpack the sequence / add an asterisk before the list *list
- [Python cold knowledge you don't know]__ all__ Role of / module import
- [recommended] 10 easy-to-use Python integrated development environments!
- Simple Python problem
- [Python crawler practice] use selenium to climb a music song and comment information~
- [Python crawler series] have a taste of 40 practical tutorials of crawlers + source code [Basic + Advanced]
- [Python reptile practice] it's too awesome to produce novels and only work as a website Porter ~ (source code attached)
- Python multithreaded sub threads' disappear 'for no reason
- Python's web crawler
- Offline installation of Python paramiko module on Linux
- Is there any scheme for user-defined data monitoring, such as whether the exception information thrown by my Python can be output to the alarm?
- Python sum can be directly passed into the list derivation
- Python machine learning 15 - detailed usage of xgboost and lightgbm (cross validation, grid search parameters, variable filtering)
- 012 Python data structure and algorithm: linked list
- 013 Python data structure and algorithm: bidirectional linked list
- 014 Python data structure and algorithm: stack and queue
- 018 Python data structure and algorithm: merge sort / binary search
- Column diagram 5 of Python drawing learning
- Python drawing learning column chart 6
- Python drawing learning column chart 7
- Seaborn of Python drawing histogram
- Seaborn of Python's box drawing
- Seaborn of Python drawing scattergram
- Seaborn of Python drawing violin
- Seaborn of Python drawing kernel density estimation graph
- Python drawing heat map Seaborn
- Pairplot function in Python visual exploration of the relationship between data features Seaborn
- Seaborn of Python drawing column graph
- Python draw Wayne diagram
- Seaborn of Python line drawing
- How to judge whether an element exists in selenium in Python syntax?
- [Python PIP special usage] PIP install -v -e. command details
- [artificial intelligence] machine learning and intelligent data processing Python uses naive Bayesian algorithm to classify spam message data sets
- Django4.0 + web + MySQL 5.7 realize simple login operation
- [Python] modules and packages