【问题标题】:Python3/Tornado/HTML file upload modifies filePython3/Tornado/HTML 文件上传修改文件
【发布时间】:2016-12-13 10:02:14
【问题描述】:

我正在尝试通过 HTML 页面上传文件。该文件应写入新文件。 Python 管理 Post。

我的post方法如下:

async def post(self, id):
    """
    Used to send a component to a node OTA
    :return:
    """
    # write to file
    path = "files/"
    fileinfo = self.request.files['filearg'][0]

    fname = fileinfo['filename']
    extn = os.path.splitext(fname)[1]
    cname = str(uuid.uuid4()) + extn
    fh = open(path + cname, 'w')
    fh.write(str(fileinfo['body']))
    fh.close()
    ...

但是,这会修改文件! 如果我将 .java 文件提供给上传表单,例如,

公共类 Test_SensorReading 扩展 UJCmp{

public static int CMP_ID = 111; //UJCmpTypes.TEST_SENSORREADING;
public static int NB_INSTR_TO_RUN = 10000;
public static int ALLOC_MEM = 10;

public static int wait_s = 10;

public static void main(){
    Test_SensorReading inst = new Test_SensorReading();
    inst.execute();
}
...

新文件中的结果是。

b'public class Test_SensorReading extends UJCmp{\n\t\n\tpublic static int CMP_ID = 111; //UJCmpTypes.TEST_SENSORREADING;\n\tpublic static int NB_INSTR_TO_RUN = 10000;\n\tpublic static int ALLOC_MEM = 10;\n\t\n\tpublic static int wait_s = 10;\n\n\tpublic static void main(){\n\t\tTest_SensorReading inst = new Test_SensorReading();\n\t\tinst.execute();\n\t}...

并且该方法的其余部分都失败了。 我怀疑文件在某处被修改是一个问题,但无法确定在哪里。基本上,新文件应该与旧文件完全相同。

【问题讨论】:

    标签: html python-3.x tornado


    【解决方案1】:

    答案很简单:我应该在 str() 命令中指定编码 utf-8。 像这样:

    str(fileinfo['body'],'utf-8')
    

    这会产生相同的文件!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 2011-04-20
      • 2014-07-28
      • 2016-05-01
      • 1970-01-01
      相关资源
      最近更新 更多