2011-08-09 30 views
1

比方说,我有2个文件的格式如下:如何按正确的顺序处理上传的文件?

<!-- the field below contains actual upload file --> 
<input type=hidden name="http://localhost:XXXX/some_unique_name_generated_by_Picasa_and_not_controlled_by_me"> 
<!-- the name of field below is equal to the uploaded above filename --> 
<input type=hidden name="DSC04310.jpg" value="description of first file"> 

<input type=hidden name="http://localhost:XXXX/some_unique_name_generated_by_Picasa_and_not_controlled_by_me"> 
<input type=hidden name="DSC04306.jpg" value="description of second file"> 

不知怎的,他们上传的时候,我让他们在不同的顺序在服务器上 - DSC04306.jpgDSC04310.jpg。我使用:

arguments = self.request.arguments() 
files_arguments = []    
for argument in arguments: 
    if 'localhost' in argument: # choose files only, not other fields 
     files_arguments.append(argument) 

但我需要按照它们在窗体中的相同顺序来处理它们。 我想到了以下解决方案:

<input type=hidden name="http://localhost:XXXX/some_unique_name"> 
<input type=hidden name="DSC04310.jpg" value="description of first file"> 
<input type=hidden name="seq_DSC04310.jpg" value="1"> 

<input type=hidden name="http://localhost:XXXX/some_unique_name"> 
<input type=hidden name="DSC04306.jpg" value="description of second file"> 
<input type=hidden name="seq_DSC04306.jpg" value="2"> 

是不是好办法?如果是这样,那么如何根据seq_FILENAME字段中的值对files_arguments中的值进行排序?

回答

1

请勿使用request.arguments(),除非您想要请求中的所有密钥(包括来自查询字符串的密钥(而不仅仅是POST))。另外,它没有被订购

取而代之,迭代request.POST值,因为它是有序的。您可以使用request.POST.iteritems()request.POST.itervalues()

for key, value in self.request.POST.iteritems(): 
    # ... 
+0

感谢,赖斯,回答我的所有GAE相关的问题)。如果我使用'iteritems',我会以与上传相同的顺序得到它吗? –

+0

这不是来自HTTP规范的东西,但实际上,是的。 – moraes

+0

我不认为订单保证首先由浏览器保存,是吗?尽管如此,我很疑惑这些字段的处理顺序可能很重要。 –