【问题标题】:Getting type error while opening an uploaded sql file打开上传的 sql 文件时出现类型错误
【发布时间】:2015-12-18 22:14:15
【问题描述】:

我正在用 django 在 python 中开发一个应用程序。用户可以上传 SQL 文件。我使用 fileField 来获取文件。但是,它没有存储在任何地方。我尝试从请求中获取它来处理文件。当我试图打开文件时,它给出了一个错误。我尝试获取上传文件的请求对象也是django设计的对象。

from mssql2postgresql.form import LoadForm

form = LoadForm(request.POST, request.FILES)
if form.is_valid():
    docFile = request.FILES['docFile']
    archivo = UploadedFile.name
    print 'este es el file', docFile

给定的错误是:

coercing to Unicode: need string or buffer, InMemoryUploadedFile found

【问题讨论】:

  • 你是这个意思吗? print 'este es el file', archivo

标签: python django


【解决方案1】:

存储在request.FILES 中的文件是流,因此它们不能只转换为字符串。要打印文件内容,必须先阅读:

docFile = request.FILES['docFile'].read()

【讨论】:

    猜你喜欢
    • 2012-05-23
    • 1970-01-01
    • 1970-01-01
    • 2017-03-14
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多