django简单上传功能

发布于 2025-02-06 20:53:37 字数 377 浏览 2 评论 0原文

获取此错误:

Exception Type: AttributeError
Exception Value: 'InMemoryUploadedFile' object has no attribute 'save'

尝试使Django函数如此简单时:

def upload(request):
    file = request.FILES["picture"]
    file.save("hello.png")
    return HttpResponse("done uploading")

注意:我知道有更长的方法来制作此简单过程,但我现在需要它像这个一样简单

Getting this error:

Exception Type: AttributeError
Exception Value: 'InMemoryUploadedFile' object has no attribute 'save'

When trying to have a Django function as simple as:

def upload(request):
    file = request.FILES["picture"]
    file.save("hello.png")
    return HttpResponse("done uploading")

Note: I know there is a longer way to make this simple procedure but I need it for now to be as simple as this one

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你曾走过我的故事 2025-02-13 20:53:37

您可以在块中读取上传的文件,然后写入所需的文件,有关详细信息,请参见文档

def upload(request):
    file = request.FILES['picture']
    with open('hello.png', 'wb+') as destination:
        for chunk in file.chunks():
            destination.write(chunk)
    return HttpResponse('done uploading')

You can read the uploaded file in chunks and write to your desired file, see the documentation for details

def upload(request):
    file = request.FILES['picture']
    with open('hello.png', 'wb+') as destination:
        for chunk in file.chunks():
            destination.write(chunk)
    return HttpResponse('done uploading')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文