【问题标题】:How can I solve TypeError: expected str, bytes or os.PathLike object, not InMemoryUploadedFile如何解决 TypeError: expected str, bytes or os.PathLike object, not InMemoryUploadedFile
【发布时间】:2021-08-31 02:17:16
【问题描述】:

我正在尝试使用 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")

【问题讨论】:

    标签: python base64


    【解决方案1】:

    发生错误是因为您试图打开 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 表示容易得多。

    【讨论】:

      猜你喜欢
      • 2021-09-12
      • 2020-11-07
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多