2016-12-13 10 views
0

HTMLページからファイルをアップロードしようとしています。このファイルは新しいファイルに書き込まれる必要があります。 PythonはPostを管理します。Python3/Tornado/HTMLファイルアップロードファイルを変更する

次のように私のポスト方法は次のとおりです。

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() 
    ... 

はしかし、これは、ファイルを変更! Iアップロードフォームに.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}... 

残りのメソッドは失敗します。 私はファイルがどこかで変更されているが、どこを特定できなかったのかと疑問に思う。基本的には、新しいファイルは古いファイルとまったく同じでなければなりません。

答えて

0

答えは簡単でした。私は、str()コマンドでエンコードutf-8を指定していたはずです。 ように:

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

これは同じファイルを生成します!

関連する問題