【问题标题】:Upload and Save an excel file with BottlePy使用 BottlePy 上传并保存一个 excel 文件
【发布时间】:2012-12-30 12:30:23
【问题描述】:

我正在使用 Bottle 框架创建一个应用程序。我需要一个上传 Excel 文件的功能。 我正在使用以下文件上传。 http://bottlepy.org/docs/dev/tutorial.html#post-form-data-and-file-uploads

在服务器端,我将文件数据作为二进制内容获取。我想将它作为 Excel 文件保存在临时文件夹中。

我是 Python 和 Bottle 的新手。任何帮助将不胜感激。

谢谢 啾啾

【问题讨论】:

    标签: python-3.x bottle


    【解决方案1】:

    您的request.files.data 对象包含有关您的 excel 文件的数据。所以你只需要创建一个临时文件夹并保存在里面。这可以使用the tempfile module 来完成

    f = tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx")
    f.write(request.files.data.file.read())
    f.close()
    

    【讨论】:

      【解决方案2】:

      我无法让像你这样的简单文件编写代码工作,所以我使用了 tempfile 模块。查看您的代码,如果代码正常,我会假设它会写入 python 文件所在的目录。尝试使用下面的代码,如果你不向 dir 传递参数,它将在当前目录中创建一个文件。

      def save_as_temp_file(data):
          with tempfile.NamedTemporaryFile(dir=settings.TEMP_PATH,
                  delete=False,
                  suffix=".xlsx") as f:
              f.write(data.file.read())
              return f.name
      

      【讨论】:

        猜你喜欢
        • 2023-03-18
        • 2021-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-07
        • 2012-12-31
        • 2019-03-02
        • 1970-01-01
        相关资源
        最近更新 更多