回答

4

伪造表单POST是可能的,但它更易于使用的Files API

编辑:

要使用网址抓取使用File API,你会写是这样的:

from __future__ import with_statement 
from google.appengine.api import files 
from google.appengine.api import urlfetch 

url = "http://www.facebook.com/somephoto.png" 
result = urlfetch.fetch(url) 
if result.status_code not 200: 
    return "some error" 

# Create the file 
file_name = files.blobstore.create(mime_type='application/octet-stream') 

# Open the file and write to it 
with files.open(file_name, 'a') as f: 
    f.write(result.content) 

# Finalize the file. Do this before attempting to read it. 
files.finalize(file_name) 

# Get the file's blob key 
blob_key = files.blobstore.get_blob_key(file_name) 

我还没有测试过 - 所以如果这不起作用,请告诉我。

此外,我相信你可以将mime_type作为'application/octet-stream',App Engine会尝试猜测正确的类型。如果这不起作用,请尝试将其更改为“image/png”。或者对于pdf,mime类型应该是'application/pdf'

+0

哦,我在看这一点,但你会怎么写的内容,比方说,一个PDF的blob对象?它似乎只支持文本。 – kennysong

+0

@jellyksong我添加了一些示例代码。 –

+0

很酷,有用!但该文件正在下载(一旦你在url中查看它)没有扩展名,所以你不知道这是一个.pdf。有没有附加文件扩展名的方法? – kennysong

0

我们实际上花了很多时间试图找出我们是否可以绕过浏览器来伪造你描述的确切类型的东西,但无济于事。我的建议是编写一些快速的后端代码,例如Kyle建议从url抓取一个文件对象,但如果你真的执着,你实际上可以用ajax伪造自己的多部分表单请求。

XMLHttpRequest POST multipart/form-dataIs it possible to fake a multipart/form-data post with a jquery ajax call?关于如何做的一些想法这

1

在顶部的文件API投票的答案是过时了,所以我写了一个要点,以解决在github这个问题。它采用图片的网址并使用请求和海报将其发布到自己的服务器上。

https://gist.github.com/jmasonherr/6301914