以下是视图中的代码:
def index(request):
if request.method == 'POST':
a=request.POST
# logging.debug(a["title"])
# logging.debug(a["file"])
#form = UploadFileForm()
form = UploadFileForm(request.POST, request.FILES)
#handle_uploaded_file(request.FILES['file'])
if form.is_valid():
handle_uploaded_file(request.FILES['file'])
return HttpResponseRedirect('/')
else:
form = UploadFileForm()
return render('upload.html', {'form': form})
def handle_uploaded_file(file):
# logging.debug("upload_here")
if file:
destination = open('/tmp/'+file.name, 'wb+')
#destination = open('/tmp', 'wb+')
for chunk in file.chunks():
destination.write(chunk)
destination.close()
Run Code Online (Sandbox Code Playgroud)
以下是模型中的代码:
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField(type="file")
Run Code Online (Sandbox Code Playgroud)
以下是upload.html中的代码:
{% block upload %}
<form enctype="multipart/form-data" method="post" action="/upload/">
{% csrf_token %}
<table>
<tr><td>
<input type="file" value="title" name="title" …Run Code Online (Sandbox Code Playgroud) 我知道,如果我想出这个或者有人告诉我,那将是一个额头拍。在发布任何问题之前,我会尝试至少三个小时并进行大量搜索。有一些提示很接近,但是我采用/尝试过的任何方法似乎都没有用。
我正在从Java中获取一个byte [],然后使用Flask通过JSON(与Gson)将其传递给python JSON。收到时,此byte []作为整数列表存储在python对象中,但是现在我需要将其发送到MySQLdb并将其存储为blob。数据内容是二进制文件数据。
如何将python整数列表[1,2,-3,-143 ....]转换为可以存储在MySQL中的内容?我已经尝试过bytearray()和array.array(),但是当我直接从对象访问列表并尝试将其转换为字符串以存储MySQLdb时,这些命令令人窒息。
任何链接或提示,我们将不胜感激。