How can I solve TypeError: expected str, bytes or os.PathLike object, not InMemoryUploadedFile
How can I solve TypeError: expected str, bytes or os.PathLike object, not InMemoryUploadedFile
我正在尝试使用 base64 对图像进行编码,以便将其作为字符串保存到数据库中。使用 django,我从 POST 请求中获取表单数据中的任何内容。为了指定图像,我使用了一个文件输入标签并将 enctype 更改为 multipart/form-data。但我收到此错误:
TypeError: expected str, bytes or os.PathLike object, not InMemoryUploadedFile
我知道还有另一个页面有同样的问题,但我觉得它不能解决我的问题,因为我不处理该文件,因为它只是一个图像。我试过访问其他网站,但它们似乎没有那么有用
这是注册表,可以看到有文件输入标签
<div class="login-cont">
<h1>Register!</h1>
<div class="form">
<form action="{% url 'register' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<input type="text" name="username" placeholder="Username" class="form-control">
<input type="text" name="fullname" placeholder="Full Name" class="form-control">
<input type="email" name="email" placeholder="Email" class="form-control">
<input type="password" name="password" placeholder="Password" class="form-control">
<input type="text" name="area" placeholder="Which state did you study in?" class="form-control">
<input type="text" name="school" placeholder="What university/college did you attend" class="form-control">
<input type="file" name="propic" class="custom-file-input" id="validatedCustomFile" placeholder="Choose photo..">
<input type="submit" value="Submit" class="btn btn-primary">
</form>
</div>
</div>
这是处理它的视图
def signup(request):
if request.method == "POST":
username = request.POST["username"]
fullname = request.POST["fullname"]
password = request.POST["password"]
email = request.POST["email"]
area = request.POST["area"]
school = request.POST["school"]
propic = request.FILES["propic"]
with open(propic, 'rb') as binary_file:
binary_file_data = binary_file.read()
base64_encoded_data = base64.b64encode(binary_file_data)
base64_message = base64_encoded_data.decode('utf-8')
print(base64_message)
try:
user = User.objects.create_user(username=username, email=email, password=password, area=area, school=school, propic=propic)
user.save()
except IntegrityError:
return render(request, "alumnas/register.html", {
'message':"Username taken"
})
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return render(request, "alumnas/register.html")
发生错误是因为您试图打开 uploaded file 并将其用作路径(查看您的回溯)。
上传的文件通常存储在内存中并已准备好工作,因此您不需要(几乎不能)直接打开它。
所以,而不是做
with open(propic, 'rb') as binary_file:
binary_file_data = binary_file.read()
做
binary_file_data = propic.read()
您还可以查看 Django 文档,了解如何work with files 使用有关使用文件的最佳实践,这可能比在数据库中存储 base64 表示形式容易得多。
我正在尝试使用 base64 对图像进行编码,以便将其作为字符串保存到数据库中。使用 django,我从 POST 请求中获取表单数据中的任何内容。为了指定图像,我使用了一个文件输入标签并将 enctype 更改为 multipart/form-data。但我收到此错误:
TypeError: expected str, bytes or os.PathLike object, not InMemoryUploadedFile
我知道还有另一个页面有同样的问题,但我觉得它不能解决我的问题,因为我不处理该文件,因为它只是一个图像。我试过访问其他网站,但它们似乎没有那么有用
这是注册表,可以看到有文件输入标签
<div class="login-cont">
<h1>Register!</h1>
<div class="form">
<form action="{% url 'register' %}" method="POST" enctype="multipart/form-data">
{% csrf_token %}
<input type="text" name="username" placeholder="Username" class="form-control">
<input type="text" name="fullname" placeholder="Full Name" class="form-control">
<input type="email" name="email" placeholder="Email" class="form-control">
<input type="password" name="password" placeholder="Password" class="form-control">
<input type="text" name="area" placeholder="Which state did you study in?" class="form-control">
<input type="text" name="school" placeholder="What university/college did you attend" class="form-control">
<input type="file" name="propic" class="custom-file-input" id="validatedCustomFile" placeholder="Choose photo..">
<input type="submit" value="Submit" class="btn btn-primary">
</form>
</div>
</div>
这是处理它的视图
def signup(request):
if request.method == "POST":
username = request.POST["username"]
fullname = request.POST["fullname"]
password = request.POST["password"]
email = request.POST["email"]
area = request.POST["area"]
school = request.POST["school"]
propic = request.FILES["propic"]
with open(propic, 'rb') as binary_file:
binary_file_data = binary_file.read()
base64_encoded_data = base64.b64encode(binary_file_data)
base64_message = base64_encoded_data.decode('utf-8')
print(base64_message)
try:
user = User.objects.create_user(username=username, email=email, password=password, area=area, school=school, propic=propic)
user.save()
except IntegrityError:
return render(request, "alumnas/register.html", {
'message':"Username taken"
})
login(request, user)
return HttpResponseRedirect(reverse("index"))
else:
return render(request, "alumnas/register.html")
发生错误是因为您试图打开 uploaded file 并将其用作路径(查看您的回溯)。
上传的文件通常存储在内存中并已准备好工作,因此您不需要(几乎不能)直接打开它。
所以,而不是做
with open(propic, 'rb') as binary_file:
binary_file_data = binary_file.read()
做
binary_file_data = propic.read()
您还可以查看 Django 文档,了解如何work with files 使用有关使用文件的最佳实践,这可能比在数据库中存储 base64 表示形式容易得多。