2010-06-03 72 views
1

如何使用python cgi將.png文件發送到flex應用程序?使用python cgi發送.png文件

在此先感謝...

+0

- 服務器或客戶端?如果你已經想出了CGI部分,那麼在Flex或HTML中就是一樣 - 只需使用CGI URL – Amarghosh 2010-06-03 12:07:49

+0

兩部分...... – Nimmy 2010-06-03 12:18:12

+0

既然你說'發送圖像':你想要在圖像控件中顯示它或者將其加載到字節數組中進行處理?無論哪種情況,您都可以將CGI url用作'src'屬性。至於CGI部分,您應該解析url並獲取圖像路徑,然後讀取該文件,並將其內容類型頭設置爲'image/png'寫入響應:我不熟悉如何操作在CGI中,所以請等待某人帶上一些代碼。 – Amarghosh 2010-06-03 12:26:02

回答

3

你的問題的Python的/ CGI側可以像這樣的事情一樣簡單,如果你只需要發送一個現有的圖像:

import sys 

# Send the Content-Type header to let the client know what you're sending 
sys.stdout.write('Content-Type: image/png\r\n\r\n') 

# Send the actual image data 
with open('path/to/image.png', 'rb') as f: 
    sys.stdout.write(f.read()) 

如果,在另一方面,你動態地,說,PIL創建圖像,你可以做一些沿着這些路線:

import sys 
import Image, ImageDraw # or whatever 

sys.stdout.write('Content-Type: image/png\r\n\r\n') 

# Dynamically create an image 
image = Image.new('RGB', (100, 100)) 
# ... etc ... 

# Send the image to the client 
image.save(sys.stdout, 'PNG') 
你要哪部分使用Flex或Python CGI幫助