在python中有一个很好的库'base64',这个模块可以根据文件名和内容对文件进行编码/解码。我正在开发一个具有上传文件功能的API。我设法编写了一些代码来编码和解码这些文件,但是通过HTTP请求发送这个编码的base64消息的正确方法是什么?
欢迎提出任何想法,如果可能的话,请显示一个小的http请求如何执行。
答案 0 :(得分:2)
我最近必须这样做,因为django-tastypie不支持multipart/form-data。 klipstein已创建Base64FileField以使用文件的base64编码来支持文件上传。
以下是基于Base64FileField的示例:
#models.py
from django.db import models
class Person(models.Model):
name = models.CharField("name", max_length=40)
avatar = models.ImageField("avatar", upload_to="uploads/person_avatars")
def __unicode__(self):
return self.name
#views.py
import json
import base64
import sys
from django.core.files.uploadedfile import SimpleUploadedFile
from django.http import HttpResponse, HttpResponseBadRequest
from django.views.decorators.csrf import csrf_exempt
from .models import Person
MAX_SIZE = 10*1024*1024 #10MB
@csrf_exempt
def save_person(request):
if (request.method == "POST" and request.META.get("CONTENT_TYPE")
== "application/json"):
data = json.loads(request.raw_post_data)
avatar = data.get("avatar")
file = base64.b64decode(avatar["file"])
if sys.getsizeof(file) > MAX_SIZE:
response = {"error": {
"code": "invalid_request",
"message": "Max file size exceeded."
}}
return HttpResponseBadRequest(json.dumps(response),
mimetype="application/json")
person = Person(name=data.get("name"), avatar=SimpleUploadedFile(
avatar["name"],
file,
getattr(avatar, "content_type", "application/octet-stream")))
person.save()
response = {"id": person.id, "name": person.name,
"avatar": person.avatar.url}
return HttpResponse(json.dumps(response), mimetype="application/json")
response = {"error": {
"code": "invalid_request",
"message": "Method Not Allowed."
}}
return HttpResponseBadRequest(json.dumps(response),
mimetype="application/json")
以下是为上述视图发布的示例JSON正文
{
"name": "Test User",
"avatar": {
"name": "myfile.png",
"file": "a long base64 encoded string ",
"content_type": "image/png"
}
}
示例响应是:
{
"id": 1,
"avatar": "/media/uploads/person_avatars/myfile.png",
"name": "Test User"
}
最好的方法是什么?
不确定这是否是最佳方式,但是,它简单明了。